- In python, we have some operations that perform the specified operations on strings.
- In this, the string must enclose with the codes that might be single or double.
- Here are some python string operations.
1. String assignment operator:
- In this operator, the string is assigned to any of the variables.
- The string may be in single or double codes.
- It is represented by " =" equal to symbol.
Syntax: string ="any variable."
Example: string1="python"
string2="java"
In the above example, we assign a "python" to string 1 and "java" to string 2 by using the" =" symbol.
# write a python program for the string assignment operator.
- str1="welcome to python"
- str2="Today topic is string operator"
- str3="python is easy language"
- print(str1)
- print(str2)
- print(str3)
output: welcome to python
Today topic is string operator
Python is easy to language
2. Cancatenate operator:
- This operator has used to cancate the strings.
- Concatenation means joining or combing the strings by using the plus symbol between the strings.
- It is represented by the symbol " +".
Example: "Hello" +"world"
- Result is : "Helloworld"
#Write a python program using string cancatenate operator.
- str1="a"
- str2="is"
- str3="alphabet"
- print(str1+str2+str3)
output: aisaphabet
3. String repetition operator:
- In this operator, the string is repeated the number of times that we declare in the code.
- It is represented by the symbol "*".
Syntax: str*n Here n is the number of times that the string can be repeated.
Example: str="apple"
str*2
The result is: apple apple
- here we declare the string as apple it should be repeated for 2 times.
#Write a python code using the string repetition operator.
- str="welcome"
- print(str*5)
output: welcome welcome welcome
4.Slicing operator:
- In this slicing operator, the strig is divided according to the given index values.
- The string can y accessed by using the index.
- It is having positive index and negative index values.
- Positive values start from 0 from left to right.
- The negative index starts from -1 from right to left.
- [:] this gives all values in the index 0 to end.
- The slicing never shows the error, if the index is out of range, it shows an empty list[].
Syntax: str[]
Example: str="string"
str[0]=s
str[1]=t
str[-1]=g
- In the above example, the s index is 0 so it returns s when we write str[0].
#write a python code for slicing operator.
- str="coding"
- print(str[0])
- print(str[-1])
- print(str[1:6])
- print(str[1:])
- print(str[:3])
- print(str[1:-4])
- print(str[-3])
output: c
g
oding
oding
cod
o
i
5. String comparison operator:
- In the string comparison operator, we compare the strings.
- In this, we have 2 operator
- It is represented by the symbol "==".
Syntax:str1==str2.
2.not equal(!=): If both strings are not the same then it true else false.
- It is represented by the symbol "!=".
Syntax: str1!=str2
#write a python coding using the python string comparison operator.
- str1="my name is A"
- str2="my name is B"
- str3="my name is A"
- str4="apple"
- str5="apple"
- print(str1==str2)
- print(str1==str1)
- print(str4==str5)
- print(str4!=str1)
- print(str3!=str2)
output: false
true
true
true
true
6. Membership operator:
- In the membership operator, we check the string that is declared in the code is present in the given list or not.
- There are 2 operators in membership.
- In: It tells that the string is present in the given list.If it is present returns true else false.
- Not in: It tells that the string is not present in the given list returns true else false.
Example: str="string"
r in the str
The result is: true
#write python code using membership operator.
- str="Hello world"
- print("H" in str)
- print('s' in str)
- print("a" not in str)
output: true
false
true
7. Escape sequence operator:
- This operator is used when an unallowed character is present in the given string.
- It is represented by the "\" symbol called backslash.
Write a python program for escape sequence operator.
- str="welcome to \"python\" classes"
- print(string)
output: welcome to "python" classes
8.String formatting operator:
- It is used to format the string as per our requirement.
- There are different types of formats.
- It is denoted by that "%" symbol.
- Each format has its own meaning.
%d: used for sign decimal integer.
%s: used for string.
%f: used for floating.
%u: used for unsigned decimal integer.
%c: used for the character.
#write a python code using the string formatting operator.
- name="abc"
- marks=78
- str1='hi % s'%(name)
- print(str1)
- str2='hi% s,my marks is %f'%(name,marks)
- print(str2)
output: hiabc,my marks is 78.000000
No comments