String methods:

  • str.capitalize(): Returns copy of the string with its first character capitalized and rest of the letters in lowercase.
#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
  • str.title()- Returns copy of string where first character in every word is upper case.
s1="example of strings"
	print (s1.title()) #Output:Example Of Strings
  • str.casefold(): Returns casefolded copy of the string. Converts string to lower case. Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string.
#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
  • str.swapcase():Returns copy of string with uppercase characters converted to lowercase and vice versa.
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
  • str.lower()-Returns copy of string in lowercase.Symbols and numbers are ignored.
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?
  • str.upper()-Returns copy of string in uppercase.Symbols and numbers are ignored.
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():Returns an encoded version of the string in byte format.
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)
  • str.startswith()- Returns True, if the string starts with specified value, otherwise returns False.

#python3 #python #string-methods #python-strings #python-programming

String methods in Python
3.00 GEEK