Built-in Functions on Python Strings

Python Tutorial Series: A tutorial on the built-in functions of Python.

Rohit Kumar Thakur
Python in Plain English
3 min readOct 31, 2021

--

Python Strings

Hello Python Enthusiasts!

In the last section of this Python Tutorial Series, we talked about Python Strings. In this section, we are going to deal with the built-in Strings functions of Python. If you do project some Python projects then these built-in strings are going to help you a lot.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

find() Function

>>> players = 'virat, dhoni, rohit, abd, bumrah, raina, sachin'
>>> players.find('rohit')
14
>>> players.find('abcd')
-1

This function is helpful when we have to find out a substring. If you get a long list of Strings and you want to find out the index of a particular substring, then you can use this function. But if you want to find out the index of something that is not present in the String list then you will get an output of -1.

capitalize(), title(), lower(), upper(), and swapcase() Functions

  • capitalize() function is used for converting the first letter of a String to the uppercase character and converting the remaining letters in a String to lowercase.
>>> "folLow ME on mEdIum".capitalize()
'Follow me on medium'
  • title() function is used for capitalizing the first letter of each word in the String and changing the remaining to lowercase.
>>> "folLow ME on mEdium".title()
'Follow Me On Medium'
  • lower() and upper() functions are used to convert all the letters of Strings to lowercase and uppercase respectively.
>>> "folLow ME on mEdium".lower()
'follow me on medium'
>>> "folLow ME on mEdium".upper()
'FOLLOW ME ON MEDIUM'
  • The swapcase() function is used to convert lowercase letters in a String to uppercase letters and vice versa.
>>> "folLow ME on mEdium".swapcase()
"folLow ME on mEdium".swapcase()

count() Function

vowels = 'AEIOUaeiou'
total = 0
for x in vowels:
total+= 'Follow Me On Medium'.count(x)
print(total)

The output will be 7. Here the count function counts the total number of vowels in the String.

replace() Function

>>> message = "HTML is a programming language"
>>> message.replace('HTML', 'Python')
'Python is a programming language'

With the help of replace function, you can replace a part of a String with another String.

strip(), lstrip(), and strip() Functions

The functions lstrip() and rstrip() remove whitespace from the beginning and end, respectively. The function strip() removes the spaces from both ends.

>>> '    Follow me on medium.'.lstrip()
'Follow me on medium'
>>> 'Follow me on medium '.rstrip()
'Follow me on medium'
>>> ' Follow me on medium '.strip()
'Follow me on medium'

islower(), isupper(), and istitle() Functions

>>> 'Python'.isupper()
False
>>> 'Python'.istitle()
True
>>> 'python'.islower()
True
>>> 'Python 123'.istitle()
True

isupper() and islower() functions check uppercase and lowercase of the String respectively. istitle() function checks for the title in Python String.

split() and partition() Functions

>>> players = 'rohit, virat, dhoni, raina, sachin'
>>> players.split(',')
['rohit', ' virat', ' dhoni', ' raina', ' sachin']

The function split() enables us to split a String into a list of Strings based on a delimiter.

The function partition divides a String into two parts based on a delimiter and returns a tuple.

>>> 'Hello. Clap for this article'.partition('.')
('Hello', '.', ' Clap for this article')

join() Function

>>> '!!!'.join(['Hello', 'Follow', 'and clap for this article'])
'Hello!!!Follow!!!and clap for this article'

It returns a String comprising elements of a sequence separated by the specified delimiter.

startswith() and endswith() Functions

>>> message = 'You are a good developer'
>>> message.startswith('I')
False
>>> message.endswith('developer')
True

These are self-explanatory Python functions.

encode() and decode() Functions

For compatibility, sometimes we convert the format of data. Something like this:

>>> message = 'Hello'.encode('utf32')
>>> message
b'\xff\xfe\x00\x00H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00'
>>> message.decode('utf32')
'Hello'

Well, that’s it for this article.

If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.

Thanks for reading.

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).

More content at plainenglish.io

--

--