Untitled Document
Untitled Document
The capitalize() method returns a string where the first character is upper
case, and the rest is lower case.
txt = "python is FUN!"
x = txt.capitalize()
print (x)
2.The casefold() method returns a string where all the characters are lower
case.
This method is similar to the lower() method, but the casefold() method is
stronger, more aggressive, meaning that it will convert more characters into
lower case, and will find more matches when comparing two strings and both
are converted using the casefold() method.
x = txt.casefold()
print(x)
3.The center() method will center align the string, using a specified character
(space is default) as the fill character.
txt = "banana"
x = txt.center(20)
print(x)
4.The count() method returns the number of times a specified value appears
in the string.
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
5.The endswith() method returns True if the string ends with the specified
value, otherwise False.
txt = "Hello, welcome to my world."
x = txt.endswith(".")
print(x)
Output:
Ture
6.The expandtabs() method sets the tab size to the specified number of
whitespaces.
txt = "H\te\tl\tl\to"
x = txt.expandtabs(2)
print(x)
Hello
7.The find() method finds the first occurrence of the specified value.
The find() method is almost the same as the index() method, the only
difference is that the index() method raises an exception if the value is not
found. (See example below)
x = txt.find("welcome")
print(x)
Output:
7
8.The format() method formats the specified value(s) and insert them inside
the string's placeholder.
The placeholder is defined using curly brackets: {}. Read more about the
placeholders in the Placeholder section below.
The format() method returns the formatted string.
9.The index() method finds the first occurrence of the specified value.
The index() method is almost the same as the find() method, the only
difference is that the find() method returns -1 if the value is not found. (See
example below)
x = txt.index("welcome")
print(x)
Output:
7
10.The isalnum() method returns True if all the characters are alphanumeric,
meaning alphabet letter (a-z) and numbers (0-9).
txt = "Company12"
x = txt.isalnum()
print(x)
11.The isalpha() method returns True if all the characters are alphabet letters
(a-z).
txt = "CompanyX"
x = txt.isalpha()
print(x)
12.The isascii() method returns True if all the characters are ascii characters
(a-z).
txt = "Company123"
x = txt.isascii()
print(x)
13.The isdecimal() method returns True if all the characters are decimals
(0-9).
This method can also be used on unicode objects. See example below.
txt = "1234"
x = txt.isdecimal()
print(x)
14.The isdigit() method returns True if all the characters are digits,
otherwise False.
txt = "50800"
x = txt.isdigit()
print(x)