Strings

String Operators

String operators represent the various types of operations that we can employ on the string type of the variables in the program. Moreover, python lets us apply numerous string operators on the python string and these are explained well in this article.

String Operators

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 the String Operators

string operators

Example No. 1: String Formatting Operator “%”

We use the String formatting operator for formatting a string as per the need. Moreover, for inserting a different type of variable along with the string, we use the “%” operator is along with the python string. However, “%” is prefixed to a different character which indicates the kind of value which we want to insert along with the string. Thus, some of the frequently different string formatting specifications we generally use:

%d – Signed decimal integer

%u – unsigned decimal integer

%c – Character

%s – String

%f – Floating-point real number

Code:

                    

name = "japan"

age = 18

marks = 25.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:

                    

Hey japan

My age is 18

Hey japan, my age is 19

Hey india, my subject mark is 25.560000

 

Browse more Topics Under Strings

Example No. 2: String slicing operator “[]”

Characters from a particular index of the string are accessible with the help of the string[index] operator. Moreover, the index is interpreted as a positive index beginning from ‘0’ from the left side. However, the negative index begins 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] – It returns a character from a positive index ‘a’ of the string. Further, it returns it from the left side as it displays in the index graph present above.

string[-a] – It returns a character from a negative index a of the string. Further, It returns it from the right side as it displays in the index graph present above.

string[a:b] – It returns characters from the positive index ‘a’ to the positive index ‘b’ as it displays in the index graph present above.

string[a:-b] – It returns characters from positive index ‘a’ to the negative index ‘b’ as it displays in the index graph present above.

string[a:] – It returns characters from the positive index ‘a’ to the conclusion.

string[:b] – It returns the characters from the beginning of the string to the positive index ‘b’.

string[-a:] – It returns the characters from the negative index ‘a’ to the conclusion of the string.

string[:-b] – It returns the characters from the beginning of the string to the negative index ‘b’.

string[::-1] – It returns a string with the inverse 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:

                    

e

r

ello

ellowo

lloworld

hello

hellowor

ld

dlrowolleh

 

FAQs on String Operations

Question 1: What is a basic string operation?

Answer: We can use the Addition operator, “+”, to concatenate the strings together. Moreover, we use the STRING function to format the data into a string.

Question 2: In Python, can we use “==” for strings?

Answer: We can perform the Python String comparison by using the equality “==” and comparison (<, >, != , <=, >=) operators.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Browse

Strings

Leave a Reply

Your email address will not be published. Required fields are marked *

Browse

Strings

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.