var1 = ‘Hello World!’
var2 = “Python Programming”
print (“var1[0]: “, var1[0])
print (“var2[1:5]: “, var2[1:5])
print (“var2[:5]: “, var2[:5])
print (var1*2)
print (var1+var2)
print (‘H’ in var1)
print (‘Hen’ in var1)
print (‘H’ not in var1)
print (‘He1′ not in var1)
print (r’\n’)
#% operator is unique to strings and makes up for the pack of having functions from C’s printf() family.
print (“My name is %s and weight is %d kg!” % (‘Zara’, 21))
#Python’s triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters. The syntax for triple quotes consists of three consecutive single or double qu# otes.
para_str = “this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up.”
print (para_str);
#Raw strings do not treat the backslash as a special character at all. Every character you put into a raw string stays the way you wrote it
print (r’C:\\no \n where’)
str=”Jai ShriRam Shri”
#Capitalizes first letter of string
print(str.capitalize());
#Returns a string padded with fillchar with the original string centered to a total of width columns.
print(str.center(20,’a’));
#The count() method returns the number of occurrences of substring sub in the range [start, end].
print(str.count(‘ri’));
print(str.count(‘ri’,7,16));
#The expandtabs() method returns a copy of the string in which the tab characters ie. ‘\t’ are expanded using spaces, optionally using the given tabsize
str = “this is\tstring example….wow!!!”
print (“Original string: ” + str)
print(“\r”)
print (“Double exapanded tab: ” + str.expandtabs(16))
print(“\r”)
# initializing string
str = “i\tlove\tgfg”
# using expandtabs to insert spacing
print(“Modified string using default spacing: “, end=””)
print(str.expandtabs())
print(“\r”)
# using expandtabs to insert spacing
print(“Modified string using less spacing: “, end=””)
print(str.expandtabs(2))
print(“\r”)
# using expandtabs to insert spacing
print(“Modified string using more spacing: “, end=””)
print(str.expandtabs(12))
print(“\r”)
#The isalnum() method checks whether the string consists of alphanumeric characters.
str = “this2016” # No space in this string
print (str.isalnum())
str = “this is string example….wow!!!”
print (str.isalnum())
#The isalpha() method checks whether the string consists of alphabetic characters only.
str = “this”; # No space & digit in this string
print (str.isalpha())
str = “this is string example….wow!!!”
print (str.isalpha())
#The method isdigit() checks whether the string consists of digits only.
str = “123456”; # Only digit in this string
print (str.isdigit())
str = “this is string example….wow!!!”
print (str.isdigit())
#The islower() method checks whether all the case-based characters (letters) of the string are lowercase.
str = “THIS is string example….wow!!!”
print (str.islower())
str = “this is string example….wow!!!”
print (str.islower())
#The isnumeric() method checks whether the string consists of only numeric characters. This method is present only on unicode objects.
#Note − Unlike Python 2, all strings are represented in Unicode in Python 3. Given below is an example illustrating it.
str = “this2016”
print (str.isnumeric())
str = “23443434”
print (str.isnumeric())
#The istitle() method checks whether all the case-based characters in the string following non-casebased letters are uppercase and all other case-based characters are lowercase.
str = “This Is String Example…Wow!!!”
print (str.istitle())
str = “This is string example….wow!!!”
print (str.istitle())
#The len() method returns the length of the string.
str = “this is string example….wow!!!”
print (“Length of the string: “, len(str))
#The lstrip() method returns a copy of the string in which all chars have been stripped from the beginning of the string (default whitespace characters).
str = ” this is string example….wow!!!”
print (str.lstrip())
str = “*****this is string example….wow!!!*****”
print (str.lstrip(‘*’))
#The max() method returns the max alphabetical character from the string str.
str = “this is a string example….really!!!”
print (“Max character: ” + max(str))
str = “this is a string example….wow!!!”
print (“Max character: ” + max(str))
#The min() method returns the min alphabetical character from the string str.
str = “www.tutorialspoint.com”
print (“Min character: ” + min(str))
str = “TUTORIALSPOINT”
print (“Min character: ” + min(str))
#The replace() method returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max. max − If this optional argument max is given, only the first count occurrences are replaced.
str = “this is string example….wow!!! this is really string”
print (str.replace(“is”, “was”))
print (str.replace(“is”, “was”, 3))
#The rfind() method returns the last index where the substring str is found, or -1 if no such index exists, optionally restricting the search to string[beg:end].
str1 = “this is really a string example….wow!!!”
str2 = “really”
print (str1.rfind(str2))
print (str1.rfind(str2, 0, 20))
print (str1.rfind(str2, 10, 0))
print (str1.find(str2))
print (str1.find(str2, 0, 20))
print (str1.find(str2, 10, 0))
#The rstrip() method returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).
str = ” this is string example….wow!!! ”
print (str.rstrip())
str = “*****this is string example….wow!!!*****”
print (str.rstrip(‘*’))
#The strip() method returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).
str = “*****this is string example….wow!!!*****”
print (str.strip( ‘*’ ))
#The method lower() returns a copy of the string in which all case-based characters have been lowercased.
str = “THIS IS STRING EXAMPLE….WOW!!!”
print (str.lower())
#The upper() method returns a copy of the string in which all case-based characters have been uppercased
str = “this is string example….wow!!!”
print (“str.upper : “,str.upper())