- 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.
# write a python program for the string assignment operator.
- str1="welcome to python"
- str2="Today topic is string operator"
- str3="python is easy 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.
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 "*".
- here we declare the string as apple it should be repeated for 2 times.
#Write a python code using the string repetition 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.
- In the above example, the s index is 0 so it returns s when we write str.
#write a python code for slicing operator.
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 "==".
- It is represented by the symbol "!=".
#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"
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.
#write python code using membership operator.
- str="Hello world"
- print("H" in str)
- print('s' in str)
- print("a" not in str)
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"
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.
#write a python code using the string formatting operator.
- str1='hi % s'%(name)
- str2='hi% s,my marks is %f'%(name,marks)