In This Article You Will Learn About Python String Method startswith(), strip(), swapcase(), title(), Translate(), upper(), zfill()
Python String Method – Before moving ahead, let’s know a little bit about methods Just(), Spilt(), Strip(), Splitlines()
startswith() method – If the string starts with the specified value, returns True, otherwise False.
Syntax - string.startswith( value, start, end ) Parameter Values - value - It is required argument. It checks if the specified value starts with. start - It is optional argument. An Integer specifying at which position to start the search. end - It is optional argument. An Integer specifying at which position to end the search.
Example 1- Check if the string start with specified value.
x = 'Hello, Python!' z = x.startswith('H') print(z)

Example 2- Check if the string start with specified phrase.
x = 'Hello, Python!' z = x.startswith('Hello') print(z)

Example 3- Example 1- Check if the string start with specified value from particular index number.
x = 'Hello, Python!' z = x.startswith('P' , 7 , 13) print(z)

strip() method – It removes spaces from beginning and at the end of the character.
Syntax - string.strip( characters ) Parameter Values - characters - It is optional argument. A set of characters to be removed.
Example 1- Use of strip() method to remove unnecessary space.
x = ' python is a programming language. ' z = x.strip() print(z)

Example 2- Use of strip() method to remove unnecessary character.
x = 'python,,,,, gjkje!,,,,' z = x.strip(', gjke!,') print(z)

swapcase() method – It returns a string where all the upper case letters are converted into lower case and lower case into upper case.
Syntax - string.swapcase() Parameter Values - No parameters.
Example 1- Convert upper case character into lower case.
x = 'HELLO, PYTHON' z = x.swapcase() print(z)

Example 2- Convert lower case character into upper case.
x = 'hello, python' z = x.swapcase() print(z)

title() method – It returns a string where the first character in every word contains upper case. If the word contains a number or a symbol, still first letter will be converted to upper case.
Syntax - string.title() Parameter Values - No parameters.
Example 1- Convert first letter of each word into upper case:
x = 'hello, python' z = x.title() print(z)

Example 2- Convert first letter of every character into upper case:
x = 'hello, python 3.7 version' z = x.title() print(z)

Example 3- Convert every first letter into an upper case after a non-alphabet letter.
x = 'hello, g2g2 and c6c6.' z = x.title() print(z)

translate() method – It returns translated version of given string. In other words, it returns specified character that is replaced with some specified character given in string.
Syntax - string. translate(table) Parameter Values - table – Requiring either a dictionary or mapping table how to replace character.
Example 1 – Replaced a specified letter ‘H’ with ‘J’
txt = "Hello Python!" mydict = txt.maketrans('H', 'J') print(txt.translate(mydict))

Example 2 – Using mapping table to replace original characters from string.
w = "Hello Python!" x = 'Hello' y = 'Jello' trans= w.maketrans(x,y) print("Translated string:" , w.translate(trans))

Example 3 – Use of mapping table with third parameter to remove original characters from string.
w = "Hello Python World!" x = 'Hello' y = 'Jello' z = 'l' trans= w.maketrans(x,y,z) print("Translated string:" , w.translate(trans))

upper() method – It converts string in upper case.
Note: It ignores symbols and numbers.
Syntax - string.upper() Parameter Values - No parameters.
Example 1- Convert string in upper case.
x = 'hello, python' z = x.upper() print(z)

zfill() method – It fills string with zeros (0) in left or beginning of the string, until it reaches the specified length.
If parameter (len) is less than string length, then no filling will be done.
Syntax - string.zfill(value) Parameter Values - len - It is required argument. Required number of zeros to fill the length of string.
Example 1- Fills string with zeros in beginning until it is 6 characters
x = '5' y = 'hello' print(x.zfill(5)) print(y.zfill(9))

Example 2- No filling of zeros. Parameter (len) is less than string length, then no filling will be done.
x = '75' z = x.zfill(1) print(z)

If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us