String methods:
#capitalize-Only first character of string is capitalized
s1="example of string methods"
print (s1.capitalize()) #Output:Example of string methods
s2="EXAMPLE OF STRING METHODS"
print (s2.capitalize())#Output:Example of string methods
s1="example of strings"
print (s1.title()) #Output:Example Of Strings
#casefold- converts all character to lower case
s1="Example Of String Methods"
print (s1.casefold()) #Output:example of string methods
s2="ß-Beta"
#ß-lowercase is equivalent to ss. casefold converts it to ss. But lower doesn't do that.
print (s2.casefold()) #Output: ss-beta
print (s2.lower())#Output: ß-beta
s1="example of strings"
print (s1.swapcase()) #Output:EXAMPLE OF STRINGS
s2="EXAMPLE OF STRINGS"
print (s2.swapcase()) #Output:example of strings
s3="Example Of Strings"
print (s3.swapcase()) #Output:eXAMPLE oF sTRINGS
s1="Example Of Strings"
print (s1.lower()) #Output:example of strings
s2="EXAMPLE OF STRINGS??"
print (s2.lower())#Output:example of strings??
s3="1.example of strings?"
print (s3.lower()) #Output:1.example of strings?
s1="Example Of Strings"
print (s1.upper())#Output:EXAMPLE OF STRINGS
s2="EXAMPLE OF STRINGS??"
print (s2.upper())#Output:EXAMPLE OF STRINGS??
s3="1.example of strings?"
print (s3.upper()) #Output:1.EXAMPLE OF STRINGS?
str.encode(encoding=”encoding”,errors=”errors”)
encoding(Optional):Default encoding is “utf-8”
errors(Optional):Default errors is “strict”.Raise unicode error.
s1= "example öf strings"
print (s1) #Output:example öf strings
#Use backslash for the character that can't be encoded
print(s1.encode(encoding="ascii",errors="backslashreplace")) #Output:b'example \\xf6f strings'
#ignores the character that can't be encoded
print(s1.encode(encoding="ascii",errors="ignore"))#Output:b'example f strings'
#replace the character that can't be encoded with the text explanining the character.
print(s1.encode(encoding="ascii",errors="namereplace"))#Output:b'example \\N{LATIN SMALL LETTER O WITH DIAERESIS}f strings'
#Replace the character that can't be encoded with the question mark
print(s1.encode(encoding="ascii",errors="replace"))#Output:b'example ?f strings'
#Replace the character that can't be encoded with xml character.
print(s1.encode(encoding="ascii",errors="xmlcharrefreplace"))#Output:b'example öf strings'
#strict-Raise Unicode Error
print(s1.encode(encoding="ascii",errors="strict"))
#Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
#errors are not mentioned.Default is strict-Raise Unicode Error.
print(s1.encode(encoding="ascii"))
#Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
True
, if the string starts with specified value, otherwise returns False.
#python3 #python #string-methods #python-strings #python-programming