Python String Functions

By: On:

In this Python Tutorial, We will learn various Python string functions. These string functions will be very much help while doing any string operations in Python.

Also, we will discuss:

  • Python string split().
  • Find the python string substring.
  • Python string find().
  • How to find the length of a string in python.
  • Python string uppercase.
  • String functions stratswith.
  • Python string count().
  • Check if string contains substring in python.
  • Python string comparison function.
  • Remove spaces from string python
  • Python the substring in a string
  • Python split multiple delimiters
  • Python count characters in a string
  • How to append to a string in python
  • Python capitalize each word in a string

If you are very much new to Python, then you can first download and install Python and you can create your first hello world program in Python.


Introduction to python string functions

Python provides a lot of built-in functions, we can manipulate strings. Here, you can see how to work with string functions with various examples. Python string is immutable which means it can’t change the original string.

Also read:

Python string split function

  • Using split() method we can break a string into pieces. These pieces returns as a list.
  • To split any string we have built-in split() which will split the string into list.
  • The function can take any string as an input and it can produce a list as an output.

Example:


  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “Welcome to Python Guide”.
  • Then we need to store the string value in some variable “value”.
  • Now print the “value” and it will give the list.

Code for python string split()

Below represents the python code to split the string.

value = 'Welcome to Python Guide'
print(value.split())
  • After writing the above Python code ( string split), Ones you will print value.split() then the output will appear as a list ‘[‘Welcome’, ‘to’, ‘Python’, ‘Guide’]’. Also, you can refer to the below screenshot.
Python string split function
python string functions

Find the python string substring

  • Substring can be created by using string slicing in python. The slicing starts with the start position and ends at the end position index.
  • To substring any string, We can use slicing which will substring the string.
  • It can take any string as an input and it will produce a substring as an output.

Example:

  • First of all, We need to take a string as a value. In the below python code, You can see, I have taken the value as “Welcome to Python Guide”.
  • Then we need to store the string value in some variable “value”.
  • Now print the “text” and it will give the required substring.

Code for Python string substring

Below represents the python code of substring.

value = 'Welcome to Python Guide'
text = value[18:]
print(text)

Here,

  • Start- The starting index of the substring and the character present in the index is known as substring. If we don’t mention the start, then it is assumed equal to 0. In the above code, I have taken 18 as a start point.
  • end- It is known as terminating index of the substring. If the end is not included, or the specified value exceeds the string length, it is assumed to be equal to the length by default. Here in the above code i have not taken the end, So by default it will take the end value.

After writing the above Python code ( string substring), Ones you will print “text” then the output will appear as a “Guide” as we have taken index from 18 to last index so, We got the last 5 character of string. Also, you can refer to the below screenshot.

python string substring

Python string find()

  • Using find() method we can find the first occurrence of the specified substring value. If the find() method returns -1 then the value is not present in the string.
  • To find any substring value from the string we have built-in find() which will find the specific value.
  • The function can take any string as an input and it can produce a integer value as an output.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “Python is easy”.
  • Then we need to store the specified substring value in some variable “a”.
  • Now print the “a” and it will give you the integer value of that index.

Code for python string find()

Below represents the python code of string find()

msg = 'Python is easy'
a = msg.find("easy")
print(a)
  • After writing the above Python code (string find), Ones you will print “a” then the output will appear as a “10” as we want to find the easy from the string. So, here index 10 is the integer value as an output. Also, you can refer to the below screenshot.
python string find
Python string find()

How to find length of a string in python

  • Using len() which is built-in function in python, We can use the len() method to get the length of the given string.
  • To find the length of string the built-in function len() methods returns the length of a string.
  • The function can take any string value as an input and it will return the integer value as an output.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “Python”.
  • Now print the “len(msg)” and it will return the number of item.

Code for python to find the length of a string

Below represents the python code to find the length of string.

msg = 'Python'
print(len(msg))
  • After writing the above Python code (string length), Ones you will print “len(msg)” then the output will appear as a “6” which is length of the string as an output. Also, you can refer to the below screenshot.
python string length
find length of a string in python

Python string uppercase

  • Using string upper() method we can convert all lowercase characters of a string into uppercase.
  • To convert any string in uppercase we have built-in upper() method which will convert lowercase to uppercase.
  • The function can take any lowercase string as an input and it can produce a string in uppercase as an output.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “welcome to python”.
  • Now print the “msg.upper()” and it will return the string in uppercase letter.

Code for python string uppercase

Below represents the python code for string uppercase.

msg = "welcome to python"
print(msg.upper())
  • After writing the above Python code (string uppercase), Ones you will print “msg.uppercase()” then the output will appear as a “WELCOME TO PYTHON” which will return a uppercase string as an output. Also, you can refer to the below screenshot.
Python string function uppercase
Python string uppercase

Python string startswith()

  • The startswith() method in python returns a boolean value. The startswith() returns true if the specified string starts with the prefix if not, it will return false. We can use this method if we want to get the first string value.
  • To get the first value from the string the built-in startswith() methods is used.
  • The function can take any string value as an input and it will return the true value if the specified substring match.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “Hi John”.
  • Then we need to store the specified substring value in some variable “store”.
  • Now print the “store” and it will return the string value as “True”.

Code for python string startswith()

Below represents the python code for string startswith()

msg = "Hi John"
store = msg.startswith('Hi')
print(store)
  • After writing the above Python code (string startswith), Ones you will print “store” then the output will appear as a “True” as an output if specified condition is true if not, then false. Also, you can refer to the below screenshot.
Python string function startswith
Python string startswith()
  • If the substring does not starts with specified value then it will return “false” as an output. You can refer to the below screenshot.
Python string function startswith

Python string count()

  • The count() method in Python returns the number of occurrences of a specified element in the list.
  • To find the occurrence of a specified substring in the given string we use count() methods which return how many times the substring is present in the string.
  • The function can take any string value as an input and it will return the specified substring value. The number of times the substring is present in it.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “John is good but john talk less”.
  • Then we need to store the specified substring value in some variable “store”.
  • Now print the “store” and it will return the substring occurrence. Which is the number of times it is present?

Code for python string count()

Below represents the python code for string count()

msg = "John is good but John talk less"
store = msg.count("John")
print(store)

After writing the above Python code (string count), Ones you will print “store” then the output will appear “2” as “John” is present two times. Also, you can refer to the below screenshot.

Python string function count
Python string count()

Check if string contains substring

  • By using “in” operator which is used as comparison operator and also the easiest way to check if a string contains a substring. This operator returns true if the string contains the character, otherwise it returns false.
  • To check whether the string is present or not we use this operator and it will return boolean value.
  • The function can take any string value as an input and it will return True or False as an output.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “Hello John, how are you”.
  • Then we need to store the specified substring value in some variable “store”.
  • Now print the “store” and it will return the value as true or false.

Code for python to check if the string contains substring

Below represents the python code to check if the string contains substring.

msg = "Hello John, how are you"
store = ('how' in msg)
print(store)
  • After writing the above Python code to check(string contains substring), Ones you will print “store” then the output will appear “True” as “how” is present in it. Also, you can refer to the below screenshot.
Python string contains substring
Check if string contains substring
  • If the string does not contains the specified value, then it will return “false” as an output. You can refer to the below screenshot.
Python string contains substring

Python string comparison

  • To compare 2 string, we have python string comparison operators that can be performed using equality (==) and different comparison like (>, <, !=) operators.
  • To compare 2 string, we have to take characters in both strings.
  • The function can take any string values as an input.
  • We need to take to 2 string value. In the below python code, You can see, I have taken the value as “Python is good”.
  • Both the strings are exactly the same, Hence they are equal. So equality operator is returning True as an output.

Comparing two strings using == (equal to) operator:

Below represents the python code string equal to comparison.

name1 = 'Python is good'
name2 = 'Python is good'
if name1 == name2:
print(name1,'is equal to',name2)
  • After writing the above Python code to check( string equal to ), Ones you will print “name1,’is equal to’,name2” then the output will appear “name1” and “name2” is equal. Also, you can refer to the below screenshot.
Python string comparison
Python string comparison

Comparing two strings using != (not equal to) operator:

In this below code I am going to use not equal to comparison operator.

Below represents the python code string not equal to comparison.

name1 = 'Python is good'
name2 = 'Python good'
if name1 != name2:
print(name1,'is NOT equal to',name2)
  • After writing the above Python code to check( string is not equal to ), Ones you will print “name1,’is NOT equal to’,name2” then the output will appear “name1” and “name2” is NOT equal. Also, you can refer to the below screenshot.
Python string comparison

Comparing two string using > (greater than) comparison operator:

Now, we will see how to compare two string functions in Python using greater than comparison operator.

Below represents the python code string is > (greater than) comparison.

name1 = 'Python is good'
name2 = 'Python good'
if name1 > name2:
print(name1,'is greater than',name2)
  • After writing the above Python code to check( string is greater than ), Ones you will print “name1,’is greater than’,name2” then the output will appear. Also, you can refer to the below screenshot.
Python string comparison

Comparing two string using < (less than) comparison operator:

Now, we will see how to compare two string functions in Python using less than comparison operator.

Below represents the python code string is < (less than) comparison:

name1 = 'Python good'
name2 = 'Python is good'
if name1 < name2:
print(name1,'is less than',name2)
  • After writing the above Python code to check( string is less than ), Ones you will print “name1,’is less than’,name2” then the output will appear. Also, you can refer to the below screenshot.
Python string comparison

Remove spaces from string python

In python, to remove spaces from a string, we have replace() method to remove all the spaces between the words, it will remove the whitespaces from the string.

Example:

string = ' Welcome to Python '
remove = string.replace(" ", "")
print(remove)

After writing the above Python code ( remove spaces from string python ), Ones you will print “remove” then the output will appear “WelcometoPython”. Here, replace() will remove all the whitespaces from the string. Also, you can refer to the below screenshot.

Remove spaces from string python

Python the substring in a string

In python, the substring in a string, we can do this by using if condition and ” in “ operator is used to check substring in a string.

Example:

string = ' Welcome to Python '
if "Welcome" in string:
print('yes')
else:
print('No')

After writing the above Python code (python the substring in a string), Ones you will print then the output will appear ” Yes “ as ” Welcome ” is present in the string. Here, the in operator will return true if the substring is present in the string and false otherwise. Also, you can refer to the below screenshot.

Python the substring in a string

Python split multiple delimiters

In python, to break the string with multiple delimiters we use re.split() method a delimiter separates the string with comma-separated values.

Example:

import re
msg = " I want\n to learn, python as\nit is* easy."
print(re.split('; | , |\n|\*', msg))

After writing the above Python code (Python split multiple delimiters), Ones you will print then the output will appear ” [‘ I want’, ‘ to learn, python as’, ‘it is’, ‘easy.’] ” Here, it will split with a delimiter comma and space. Also, you can refer to the below screenshot.

Python split multiple delimiters

Python count characters in a string

In python, to count the total number of character in the string we will use for loop to iterate every character in a string.

Example:

my_string = "Hello all"
total = 0
for val in my_string:
total = total + 1
print("Total Number of Characters = ", total)

After writing the above Python code (python count characters in a string), Ones you will print then the output will appear ” Total Number of Characters = 9 “. Here, for loop will iterate every character in the string and it will increment the total value for each character. Also, you can refer to the below screenshot

Python count characters in a string

How to append to a string in python

In python, to append the string in python we will use the ” + ” operator and it will append the variable to the existing string.

Example:

name = 'Misheil'
salary = 10000
print('My name is ' + name + 'and my salary is around' + str(salary))

After writing the above Python code (how to append to a string in python), Ones you will print then the output will appear ” My name is Misheil and my salary is around 10000 “. Here, the ” + ” operator is used to append the variable. Also, you can refer to the below screenshot append to a string in python.

How to append to a string in python

Python capitalize each word in a string

In python, to capitalize each word in a string we will use the built-in method upper() to capitalize all the letters in the string.

Example:

my_string = 'python guides'
print(my_string.upper())

After writing the above Python code (python capitalize each word in a string), Ones you will print “my_string.upper()” then the output will appear ” PYTHON GUIDES “. Here, the string.upper() is used to capitalize each word in string. Also, you can refer to the below screenshot to capitalize each word in a string.

Python capitalize each word in a string

You may also like following Python tutorials:


In this Python tutorial, we learned about various string functions in python. As discussed we have covered with these below Python string functions.

  • Python string split().
  • Find the python string substring.
  • Python string find().
  • How to find the length of a string in python.
  • Python string uppercase.
  • String functions stratswith.
  • Python string count().
  • Check if string contains substring in python.
  • Python string comparison function.
  • Remove spaces from string python
  • Python the substring in a string
  • Python split multiple delimiters
  • Python count characters in a string
  • How to append to a string in python
  • Python capitalize each word in a string

I hope now you can work with Python String Functions.

Leave a Comment