String Operations In Python

Introduction

 
In this article, we will learn how to perform operations on a string in Python, with examples.
 
Let's start.
 

String

 
String is an array of bytes that represent the Unicode characters in Python. Python does not support character datatype. A single character also works as a string.Python supports writing the string within a single quote('') and a double quote("").
 
Example
 
"Python" or 'Python' 
 
Code
  1. SingleQuotes ='Python in Single Quotes'  
  2. DoubleQuotes ="Python in Double Quotes"  
  3. print(SingleQuotes)  
  4. print(DoubleQuotes)  
Output
 
 
A single character is simply a string with a length of 1. The square brackets can be used to access the elements from the string.
 

print()

 
This function is used for displaying the output or result on the user screen.
 
Syntax
 
print('Text or Result')  or print("Text or Result')
 

Indexing in String

 
It returns a particular character from the given string.
 
Syntax
 
getChar = a[index] 
  1. message = "Hello, Python!"  
If I want to fetch the characters of the 7th index from the given string.
 
Syntax
 
print(string[index])
 
Code
  1. print(message[7])  
Output
 
As we know, many programming languages always provide positive indexing. If we want to fetch a particular character or substring from a string from a specific range or particular index, then we always use positive indexing but we can't use negative indexing. Python provides negative and positive indexing. Try to understand the below code.
 
Code
  1. #Text            =   P Y T H O N   
  2. # Positive Index =   0 1 2 3 4 5   
  3. # Negative Index = -(6 5 4 3 2 1)  
In the above code, you can see both positive and negative indexing of the text PYTHON, but in negative-index I don't want to repeat the negative sign(-) again and again, each and every digit, so I have used a negative sign before brackets. It means that here each and every index contains a negative sign before itself. 
 
But one more important thing is that a positive index starts from 0 but a negative index starts from -1. 
 
Let's see the positive index and then negative index example one by one.
 
Now start first with positive index,
 
Code
  1. Text = 'PYTHON'  
  2. print(Text[3])  
Output
 
 
As you can see above in the  PYTHON positive indexing,  Index 3 contains H. Which displayed on output.
 

Negative Index

 
If I want to fetch characters from a given string of -4th index.
 
Syntax
 
print(Text['Pass index']) 
 
Code
  1. Text = 'PYTHON'  
  2. print(Text[-4])  
Output
 
 

Substring

 
Get substring (specific range from and to index) from given string use index with a colon.
 
Syntax
 
string[from:to] 
 
Code
  1. message = 'Hello, Python'  
  2. print(message[7:10])  
Output
 
 
 

strip()

 
This function is used to remove white spaces from the given string.
 
Syntax
 
stringVariable.strip()
 
Code
  1. message ="    Welcome Python.  "  
  2. print (message.strip())  
Output
 
 

lower()

 
This function is used to convert a string into lower case.
 
Syntax
 
stringVariable.lower()
 
Code
  1. message ="Welcome Python!"  
  2. print (message.lower())  
Output
 
 

upper()

 
This function is used to convert a string into uppercase.
 
Syntax
 
print(string.upper())
 
Code
  1. message ="Python tutorial with dotnettechpoint.com"  
  2. print (message.upper())  
Output
 
 

len()

 
This function returns the length of the given string.
 
Syntax 
 
len(stringVariable) 
 
Code
  1. message ="Welcome Python!"  
  2. print (len(message))  
Output
 
 
 

replace()

 
This function is used to replace a string with another string.
 
Syntax
 
stringVariable("replaceString","withnewString")
 
Code
  1. message ="Python with dotnettechpoint.com"  
  2. print (message.replace("Python","Learn Python"))  
Output
 
 

split()

 
This function splits the string into a substring if any separator is available.
 
Syntax
 
string_variable.split("seperator")
 
Code
  1. message ="Python, tutorials, with, dotnettechpoint.com"  
  2. print (message.split(","))  
Output
 
 
 

title()

 
This function returns the first letter of each word in uppercase.
 
Syntax
 
stringVeriable.title()
 
Code
  1. message ="python tutorials with dotnettechpoint.com"  
  2. print (message.title())  
Output
 
 
In the above code the first letter in each word is the lower case; after using title(), it converts the whole text into title format.
 

capitalize()

 
This function is used for converting the first character into an upper case. 
 
Syntax
 
variable.capitalize() 
 
Code
  1. message ="python tutorials with dotnettechpoint.com"  
  2. print (message.capitalize())  
Output
 
 
 

count()

 
This function returns the occurrence of subsisting in a string.
 
Syntax 
 
variable.count('string')
 
Code
  1. message ="python tutorials with dotnettechpoint.com"  
  2. print (message.count('t'))  
Output
 
 
 

find()

 
This function returns in which index your search text is available in the given string.
 
Note
  1. If find() function returns -1 then search text is not found.
  2. The find() method is the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.
Syntax
 
stringVariable.find("searchText")
 
Code
  1. message ="Python tutorials with dotnettechpoint.com"  
  2. print (message.find('with'))  
Output
 
 
 

join() 

 
This function appends particular things in all given items.
 
Syntax
 
print("-".join ("Python Tutorials")
 
Code
  1. message ="Python Tutorials"  
  2. print(" ".join(message))  
Ouput
 
 
 

Concatenation of strings

 
We can concatenate two or more than two string in python using + operator but we can not concatenate a string with another data type, when we try to concatenate then it throws an error.
 
Code
  1. first_string ="Python"  
  2. last_string = "Tutorial"  
  3. print (first_string +" " +last_string)  
Output
 
 
 
Concatenate string with another datatype --  let's see what happens.
  1. first_string ="Python "  
  2. middle_string =3  
  3. last_string = "Tutorial"  
  4. print (first_string +" "+middle_string+" " +last_string)  
Output
 
 
 
In the above output error it clearly states we can concatenate only string not int. Now what is the solution to this problem? We can solve this problem to convert int into string or pass middle_string as a string and then concatenate it.
 
Solution 1
  1. first_string ="Python "  
  2. middle_string ="3"  
  3. last_string = "Tutorial"  
  4. print (first_string +" "+middle_string+" " +last_string)  
Output
 
 
 
Solution 2
  1. first_string ="Python "  
  2. middle_string =3  
  3. last_string = "Tutorial"  
  4. print (first_string +" "+str(middle_string)+" " +last_string)  
Output
 
 

Summary

 
I hope this article helps you when performing string operations in Python.


Recommended Free Ebook
Similar Articles