A Beginner Tutorial To Python String Method

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)
python-string-method
As it is shown clearly that string starts with ‘H’, therefore it returned True.

Example 2- Check if the string start with specified phrase.

x = 'Hello, Python!'
z = x.startswith('Hello')

print(z)
python-string-method
As it is shown clearly that string starts with specified phrase ‘Hello’, therefore it returned True.

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)
python-string-method
As it is shown clearly that string starts with ‘P’ between the range, therefore it returned True.

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)
python-string-method
As it is shown clearly that it removed whitespaces from beginning and end of the string.

Example 2- Use of strip() method to remove unnecessary character.

x = 'python,,,,, gjkje!,,,,'
z = x.strip(', gjke!,')

print(z)
python-string-method
As it is shown clearly that it removed remove unnecessary character from string.

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)
python-string-method
As it is shown clearly that it converted upper case character into lower case.

Example 2- Convert lower case character into upper case.

x = 'hello, python'
z = x.swapcase()

print(z)
python-string-method
As it is shown clearly that it converted lower case character into upper case.

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)
python-string-method
As it is shown clearly that it converted every first letter of string into upper case.

Example 2- Convert first letter of every character into upper case:

x = 'hello, python 3.7 version'
z = x.title()

print(z)
python-string-method
As it is shown clearly that it converted every first letter of string into upper or title case.

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)
python-string-method
As it is shown clearly that it convert every first letter into an upper case after a non-alphabet letter.

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))
python-string-method
As it is shown clearly that it returned specified character that is replaced with some specified character given in string. 

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))
python-string-method
As it is shown clearly that it replaced original string to new string.

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))
python-string-method
As it is shown clearly that it replaced original string in new string

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)
python-string-method
As it is shown clearly that it converted whole string in upper case string.

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))
python-string-method
As it is shown clearly that it filled string with zeros in the beginning of the string according to length of the string.

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)
python-string-method
As it is shown clearly that the length of the string is more than zfill() method value, therefore it did not fill any zero.

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

Like us

1 thought on “A Beginner Tutorial To Python String Method”

Leave a Comment

Stay in the loop

codingstreets