Updated April 15, 2023
Introduction to String Operators in Python
In python, String operators represent the different types of operations that can be employed on the program’s string type of variables. Python allows several string operators that can be applied on the python string are as below:
- Assignment operator: “=.”
- Concatenate operator: “+.”
- String repetition operator: “*.”
- String slicing operator: “[]”
- String comparison operator: “==” & “!=”
- Membership operator: “in” & “not in”
- Escape sequence operator: “\.”
- String formatting operator: “%” & “{}”
Examples of String Operators in Python
In the following article, we will learn how to perform operations on a string in Python, with examples.
Example #1 – Assignment Operator “=”
Python string can be assigned to any variable with an assignment operator “= “. Python string can be defined with either single quotes [‘ ’], double quotes[“ ”] or triple quotes[‘’’ ‘’’]. var_name = “string” assigns “string” to variable var_name.
Code:
string1 = "hello"
string2 = 'hello'
string3 = '''hello'''
print(string1)
print(string2)
print(string3)
Output:
Example #2 – Concatenate Operator “+”
Two strings can be concatenated or join using the “+” operator in python, as explained in the below example code:
Code:
string1 = "hello"
string2 = "world "
string_combined = string1+string2
print(string_combined)
Output:
Example #3 – String Repetition Operator “*”
The same string can be repeated in python by n times using string*n, as explained in the below example.
Code:
string1 = "helloworld "
print(string1*2)
print(string1*3)
print(string1*4)
print(string1*5)
Output:
Example #4 – String slicing operator “[]”
Characters from a specific index of the string can be accessed with the string[index] operator. The index is interpreted as a positive index starting from 0 from the left side and a negative index starting from -1 from the right side.
String | H | E | L | L | O | W | O | R | L | D |
Positive index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Negative index | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
- string[a]: Returns a character from a positive index a of the string from the left side as displayed in the index graph above.
- string[-a]: Returns a character from a negative index a of the string from the right side as displayed in the index graph above.
- string[a:b]: Returns characters from positive index a to positive index b of the as displayed in index graph above.
- string[a:-b]: Returns characters from positive index a to the negative index b of the string as displayed in the index graph above.
- string[a:]: Returns characters from positive index a to the end of the string.
- string[:b] Returns characters from the start of the string to the positive index b.
- string[-a:]: Returns characters from negative index a to the end of the string.
- string[:-b]: Returns characters from the start of the string to the negative index b.
- string[::-1]: Returns a string with reverse order.
Code:
string1 = "helloworld"
print(string1[1])
print(string1[-3])
print(string1[1:5])
print(string1[1:-3])
print(string1[2:])
print(string1[:5])
print(string1[:-2])
print(string1[-2:])
print(string1[::-1])
Output:
Example #5 – String Comparison Operator “==” & “!=”
The string comparison operator in python is used to compare two strings.
- “==” operator returns Boolean True if two strings are the same and return Boolean False if two strings are not the same.
- “!=” operator returns Boolean True if two strings are not the same and return Boolean False if two strings are the same.
These operators are mainly used along with if condition to compare two strings where the decision is to be taken based on string comparison.
Code:
string1 = "hello"
string2 = "hello, world"
string3 = "hello, world"
string4 = "world"
print(string1==string4)
print(string2==string3)
print(string1!=string4)
print(string2!=string3)
Output:
Example #6 – Membership Operator “in” & “not in”
Membership operator is used to searching whether the specific character is part/member of a given input python string.
- “a” in the string: Returns boolean True if “a” is in the string and returns False if “a” is not in the string.
- “a” not in the string: Returns boolean True if “a” is not in the string and returns False if “a” is in the string.
A membership operator is also useful to find whether a specific substring is part of a given string.
Code:
string1 = "helloworld"
print("w" in string1)
print("W" in string1)
print("t" in string1)
print("t" not in string1)
print("hello" in string1)
print("Hello" in string1)
print("hello" not in string1)
Output:
Example #7 – Escape Sequence Operator “\”
To insert a non-allowed character in the given input string, an escape character is used. An escape character is a “\” or “backslash” operator followed by a non-allowed character. An example of a non-allowed character in python string is inserting double quotes in the string surrounded by double-quotes.
1. Example of non-allowed double quotes in python string:
Code:
string = "Hello world I am from "India""
print(string)
Output:
2. Example of non-allowed double quotes with escape sequence operator:
Code:
string = "Hello world I am from \"India\""
print(string)
Output:
Example #8 – String Formatting Operator “%”
String formatting operator is used to format a string as per requirement. To insert another type of variable along with string, the “%” operator is used along with python string. “%” is prefixed to another character indicating the type of value we want to insert along with the python string. Please refer to the below table for some of the commonly used different string formatting specifiers:
Operator | Description |
%d | Signed decimal integer |
%u | unsigned decimal integer |
%c | Character |
%s | String |
%f | Floating-point real number |
Code:
name = "india"
age = 19
marks = 20.56
string1 = 'Hey %s' % (name)
print(string1)
string2 = 'my age is %d' % (age)
print(string2)
string3= 'Hey %s, my age is %d' % (name, age)
print(string3)
string3= 'Hey %s, my subject mark is %f' % (name, marks)
print(string3)
Output:
Conclusion
This tutorial provided an in-depth understanding of different string operators used in python, which includes string assignment, string repetition, string slicing, string concatenation, string comparison, string formatting, membership, escape sequence, etc. The concept discussed in these chapters is useful for dealing with string-related things in any real-life python application project.
Recommended Articles
We hope that this EDUCBA information on “String Operators in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.