In this article, we are going to learn about the basic Python String Operations Examples. We will learn about the string operations like string indexing, string slicing, string concatenation, string formatting, updating possibilities, etc with the help of examples:
1. Python String Indexing
The string in python can be accessed by indexing it from the 0th index to (n-1)the index where n is the total length of the string. So you can use only numbers between 0 to (n-1) as an index.The negative index access element from end not beginning -1 last element,-2 second last element and so on.
You can use ‘len(…)’ function to find the length of your string
PlayerName[-len(PlayerName)] #output : G first element
IndexError: string index out of range :
Accessing a character index that is greater than the string length then the program raises IndexError: string index out of range. Raise type error if use float or other datatypes.
Program Example to access string element by index
#using postive index print("Postive Index:") print(PlayerName) print(PlayerName,"\n") #using negative index print("Negative index:") print(PlayerName[-1]) print(PlayerName[-1])
Postive Index: o M Negative index: r r
2. Python Strings Slicing
Slicing means if you want to access a sub-string or sequence of characters from a string by using ‘colon'(:) operator. We can use the ‘colon'(:) operator and specify the starting and ending index of the substring that we want to slice.
The use of the ‘colon'(:) operator needs to be done with care as this can cause out-of-range errors if we try to pass an index that is exceeding the length of the original string.
where ‘i’ is the starting index and ‘j’ is the ending index. Both ‘i’ and ‘J should be less than ‘n’ where n is the length of the string Variable.
stringVariable = "Sachin was a Great cricketer" print('Whole string is = ',stringVariable[:]) print('slicing between 2-6:',stringVariable [2:6]) print('slicing between -7-1 :',stringVariable[-7:-1])
Whole string is = Sachin was a Great cricketer slicing between 2-6: chin slicing between -1-7 : ickete
3.Python String Concatenation
We can concatenate the strings in python by using the “+” operator. This is like the simple joining of multiple strings to form a single string.
Python String Concatenation
var1 = "Sachin," var2 = "is the" var3 = "Greatest Cricketer." fullString = var1 + var2 + var3 print(fullString)
If you see the output of this simple concatenation is:
Sachin is the Greatest Cricker.
4. Python String Repeat
In Python, Strings can be repeated with the help of the asterisk (*) operator. This operator works like simple multiplication of strings. Let us understand this with the help of the below example:
var1 = "Sachin" print(var1*4)
If we check the output the original string Sachin got multiplied and it has created 4 Sachin strings for us.
5. Python string comparing
The string comparisons in Python can be easily done by using the “==” operator. In other languages, we use the == and != to compare the numbers, but in Python, these operators can be used with strings also. Using these operators we can check if two strings are equal or even we can check non-equality.
If the strings comparison results truly it means the strings are equal. But if it returns false means the strings are not equal.
Program example String comaparsion
print("Sachin" == "Sachin") print("Sachin" == "Dravid") print("100" == "99")
True False False
6. Python String membership
In python to check a character exist the IN operator is used. It returned TRUE if the character exists else returns False.
stringVariable = "Sachin" if 'S' in stringVariable : print('s is exist in string') if 'S' not in stringVariable: print('s is not exist in string')
s is exist in string
7. Python string formatting
The % operator is used as a placeholder to insert another string into the original string. The % operator is a prefix to the type character that specifies the type of value to be inserted.
- %s : for string
- %c : for charcater
- %i : signed decimal integer
- %u : unsigned decimal integer
- %e : for exponential notation
stringVariable = "Sachin" Greet = "Hi %s" % stringVariable print(Greet)