String Methods
'''
Strings are immutable.
String methods work on our existing string and produce another string as per our requirement, but do not change our existing string.
'''
name = "Sindhu"
print(len(name)) #output = 6
#1.upper()
print(name.upper()) #output = SINDHU
#It converts the string into an upper case.
#2.lower()
print(name.lower()) #output = sindhu
#It converts the string into a lower case.
#3.rstrip()
name1 = "Sindhu!!!"
print(name1.rstrip("!")) #output = Sindhu
name2 = "Shilpa??"
print(name2.rstrip("?")) #output = Shilpa
name3 = "@Prasad"
print(name3.rstrip("@")) #output = @Prasad
#It removes only the trailing characters.
#It does not remove the leading characters.
#4.replace()
print(name.replace("Sindhu" , "Asish")) #output = Asish
name4 = "Sindhu @ Sindhu"
print(name4.replace("Sindhu" , "Asish")) #output = Asish @ Asish
#It replaces all datas of a string with another string that we want to replace.
#5.split()
print(name4.split(" ")) #output = ['Sindhu' , '@' , 'Sindhu']
#It splits the given string and returns the separated strings as list items.
#6.capitalize()
BlogHeading = "introduction to python"
print(BlogHeading.capitalize()) #output = Introduction to python
BlogHeading1 = "inTroductioN tO pytHon"
print(BlogHeading1.capitalize()) #output = Introduction to python
#It turns only the first character of the string into upper case and the rest other characters into lower case.
#It has no effect if any of the characters of the string is already upper case or lower case.
#7.center()
str = "Welcome to the console!!"
print(len(str)) #output = 24
print(str.center(50))
#output = Welcome to console!!(after 25 spaces from the starting)
print(len(str.center(50))) #output = 50
#It aligns the string to the center as per the instructions given by the user.
#8.count()
print(name4.count("Sindhu")) #output = 2
#It counts the number of times the given value has occurred within a given string.
#9.endswith()
print(str.endswith("!")) #output = True
#It checks if the string ends with a given value.If yes,then returns True, else returns False.
#We can also check for a value in-between the string by providing start and end index positions.
print(str.endswith("to", 4,10)) #output = True
#10.find()
str1 = "Sindhu is a beautiful girl. Sindhu is an intelligent girl."
print(str1.find("is")) #output = 7
#It searches for the first occurrence of the given value in the string and returns its index position at where it is present.
print(str1.find("the")) #output = -1
#If the given value is absent from the string, then it returns -1
#11.index()
print(str1.index("is")) #output = 7
#print(str1.index("the")) #output = valueError:substring not found
#12.isalnum()
str2 = "Iam23yearsold"
print(str2.isalnum()) #output = True
str3 = "I am 23 years old."
print(str3.isalnum()) #output = False
#It returns True only if the entire string only consists of A-Z, a-z, 0-9.
#It returns False if the string consists of any other characters or space.
#13.isalpha()
str4 = "MynameisSindhu"
print(str4.isalpha()) #output = True
print(str2.isalpha()) #output = False
print(str3.isalpha()) #output = False
#It returns True only if the entire string only consists of A-Z, a-z.
#It returns False if the strinf consists of any other characters or space or 0-9.
#14.islower()
str5 = "my name is sindhu"
print(str5.islower()) #output = True
str6 = "My name is Sindhu"
print(str6.islower()) #output = False
print(str4.islower()) #output = False
#It returns True only if all the characters of the string are in lower case, else it returns False.
#15.isprintable()
str6 = "Wish you a Merry Christmas"
print(str6.isprintable()) #output = True
#It returns true if all the values within the string are printable.
str7 = "Wish you a Merry Christmas \n"
print(str7.isprintable()) #output = False
#It returns False if any of the characters within the string(here \n) are not printable.
#16.isspace()
str8 = " " #using spacebar
print(str8.isspace()) #output = True
str9 = " " #using tab
print(str9.isspace()) #output = True
str10 = "" #no space
print(str10.isspace()) #output = False
# It returns True only if the string contains white space(either using spacebar or tab),else returns False.
#17.istitle()
str11 = "Sindhupriya Choudhury"
print(str11.istitle()) #output = True
str12 = "sindhupriya choudhury"
print(str12.istitle()) #output = False
str13 = "sinDhupriyA ChoudHury"
print(str13.istitle()) #output = False
#It returns True only if the first letter of each word of the string is capitalized, else returns False.
#18.isupper()
str14 = "SINDHUPRIYA CHOUDHURY"
print(str14.isupper()) #output = True
#It returns True only if all the characters of the string are capitalized, else it returns False.
#19.startswith()
str15 = "Sindhu is a beautiful girl"
print(str15.startswith("Sindhu")) #output = True
print(str15.startswith("girl")) #output = False
#It checks if the string starts with a given value. If yes, it returns True, else it returns False.
#20.swapcase()
print(str15.swapcase())
#output = sINDHU IS A BEAUTIFUL GIRL
#It converts the upper case into lower case and vice-versa.
#21.title()
print(str15.title()) #output = Sindhu Is A Beautiful Girl
#It capitalizes the first character of each word within a string.
Comments
Post a Comment