Strings In Python

Introduction

 
Python string is one of the common and important data types in the language. In this article, I am going to explain how we can use and manipulate strings in Python.
 
The string is a collection of characters. Strings are immutable, that is, once it is defined, it cannot be changed or updated hence strings are immutable.
 
The indexes of a string begin from 0 to length-1 in forwarding direction and -1,-2,……-length in the backward direction. For example,
Strings in Python
 

Accessing a string

 
You can access string by using its index number like below.
  1. name=”Yash Ghatge”  
  2. print(name[0])  
  3. print(name[-1])  
Accessing String
 
You can also access each character of a string using a For loop, like below.
  1. >>> name="Yash Ghatge"  
  2. >>> for ch in name:  
  3.     print(ch,'*',end='')  
Accessing String
  

String Operators

 
String Operators
 

Concatenation Operator (+)

 
The + operator creates a new string by joining the two operand strings like below.
  1. >>> "Yash" + "Ghatge"  
  2. Will result = 'YashGhatge'  
One interesting point from the above code is + operator can work with numbers and strings separately for addition and concatenation respectively. But in the same expression, you cannot combine numbers and strings as operands with a + operator. For example -
  1. 7+7 = 14            valid  
  2. 7’+’7’=’77’        valid  
  3. 7’+7               invalid, it will produce the error  

Replication Operator ( * )

 
It is used to create a new string that is a number of repetitions of the input string.
 
Syntax
 
number*string or string*number
 
Example
  1. 3*'Yash'   will give YashYashYash  
  2. 'Yash'*3   will give YashYashYash  

in operator

 
This operator returns True if the substring or character is present in the string, otherwise, it will return False.
 

not in operator

 
This operator returns True if the substring or character is not present in the string, otherwise, it will return False.
 
Examples
  1. >>> 'j' in 'japan'  
  2. True  
  3. >>> 'jap' in 'japan'  
  4. True  
  5. >>> 'jap' not in 'japan'  
  6. False  
However, both operators match the result as per case sensitive.
 

Comparison Operator

 
All comparison operators (relation operators >, <, >=, <=, ==, !=) apply to string also. The comparison using these operators is based on the standard character-by-character comparison rules for Unicode.
 
For example,
  1. “a”==”a”        will give True  
  2. “abc”==”abc”    will give True  
  3. “abc”!=”Abc”    will give True  
  4. “a”<=”A”        will give False because the ASCII value of lower case letter is higher than upper case letter.  
Comparison Operator  

Concatenation of Strings

 
As we can see one operator for concatenation i.e. +, but there are also some other ways to combine more than one string like,
 
Concatenation of string
 

Join() Function

 
It is used to return a string that has string elements joined by a separator.
 
Syntax
 
string.join(sequence)
 
Example
  1. >>> string1="*"  
  2. >>> sequence=("1","2","3","4")  
  3. >>> print(string1.join(sequence))  
join function in python
 

% operator

 
It is used to combine two or more than two strings. 
  1. >>> str1="Hi!"  
  2. >>> str2="Yash"  
  3. >>> str3="%s %s" %(str1,str2)  
  4. >>> str3  
Mod operator in python
 

format() function

 
This function also combines two or more string and returns a string.
  1. >>> str1="Hi"  
  2. >>> str2="Yash"  
  3. >>> str3="{} {}".format(str1,str2)  
  4. >>> str3  
format function in python
 

f-string

 
This function is used to concatenate string and return a string.
  1. >>> str1="Hello"  
  2. >>> str2="Yash"  
  3. >>> str3=f'{str1}{str2}'  
  4. >>> str3  
fstring in python
 

Conclusion

 
I hope I was able to properly explain some basic topics of string along with string operators and string concatenation. In my next article, I will explain some other string concepts like string slicing, string methods, etc. 
 
All the queries related to this article are always welcome. Thanks for reading.


Similar Articles