String manipulation methods
Python has many methods (out-of-the-box functions) for working with strings. Many of them are called using dot notation and are called
methods. A complete list of string manipulation methods can be found online.
Let's get acquainted with some of them.
s = "aAbBcC"
sUp = s.upper()
# sUp = "AABBCC" - a method that translates
# uppercase each character of the string
sLow = s.lower()
# sLow = "aabbcc" - a method that translates
# lowercase each character of the string
To the left of the dot is the name of the string (or the string itself in quotation marks) to which the method is to be applied, and to the right of the dot is the name of the method. The method is always written with parentheses. There can be any parameters inside the brackets if they are needed.
Previously, we already used the method of working with strings when we displayed data on the screen in a certain format - the format()
method
a = 4
b = 5
print("{}+{}={}".format(a,b,a+b)) # 4+5=9
Another useful method isdigit()
is a method to check if all characters of a string are digits, it returns a boolean value (True or False).
s = "ab1c"
print(s.isdigit()) #False
s = "123"
print(s.isdigit()) #True
The useful strip()
method allows you to remove spaces at the beginning and end of a string
s = " ab 1c "
print('s=', s.strip()) # s=ab 1c