22
loading...
This website collects cookies to deliver better user experience
"hello"
) to get a desired result.Returns a copy of the string with its first letter capitalized.
name = "john"
print(name.capitalize())
>>> John
Returns a copy of the string with all the characters converted to lowercase.
name = "Jack Ma"
print(name.lower())
>>> jack ma
use upper() for uppercase conversion
counts the number of times "sub" appeared in the string, the "start" and "end" options determines where to start counting and where to end the count.
name = "johndoe"
print(name.count("o")) # 'o' appeared 2 times
>>> 2
Returns True if all characters in the string are alphabetic and there is at least one character, otherwise returns False.
name = "johndoe"
print(name.isalpha())
>>> True
name = ""
print(name.isalpha())
>>> False # name is an empty string
name = "num34"
print(name.isalpha())
>>> False
You can also use isdigit(), isdecimal(), isnumeric(), etc...
Returns True if all characters in the string are lowercase.
name = "johndoe"
print(name.islower())
>>> True
You can also use isupper(), to check for uppercase (Capital letters)
Returns a copy of the string with all "old" values replaced by "new". "count" determines how many times it should be replace (it is optional)
name = "johndoe was good"
print(name.replace("o", "z"))
>>> jzhndze was gzzd
quote = "music is life"
print(quote.replace("i", "non", 2))
>>> musnonc nons life
Returns True if the string ends with the specified suffix, otherwise returns False. The "start" and "end" options determines where to start checking for the suffix and where to end the search.
name = "johndoe"
print(name.endswith("o"))
>>> False
language = "French"
print(language.endswith("ch"))
>>> True
Returns a list of the words in the string, using "sep" as the delimiter(splitting) symbol.
name = "life is good"
print(name.split(" ")) # splits the string by space
>>> ["life", "is", "good"]
Thanks for reading, let me know if I should add more. Hope to hear your feedback.