String Operators
M3-R5.1 · Chapter 5: Sequence Data Types · 4 min read
String Operators: A string operator in Python is an operator used to perform operations on strings, such as combining, repeating, or checking membership. These operators work specifically with string data types. there are following types of string operators
A. Basic Operators:
B. Membership Operators ( in, not in)
C. Comparison Operators (==, !=, <=, >, >=)
A. Basic Operators: There are two basic operators of strings:
i. String concatenation Operator (+)
ii. String repetition Operator (*)
String concatenation Operator: The + operator is use for creates a new string by joining the two operand strings.
EXAMPLE:
|
>>>“karamraji” + “institute” will result karamrajiinstitute >>>“1” + “2” will result into “12” >>>“123” + “abc” will result into “ 123abc” Note: we cannot add number and string using + operator >>>7+’4’ # unsupported operand type(s) for +: 'int' and 'str' It is invalid and generates an error |
>>> s1=”jai” >>> s2=”prakash” >>> s1+s2 jaiprakash >>> s1 +s2 jai prakash |
>>> s3=s1+ s2 >>>s3 jai prakash |
String repetition Operator: It is also known as String replication operator. It requires two types of operands- a string and an integer number.
5*”@” will result into “@@@@@”
“go!”*3 will result “go! go! go!”
Note: We cannot multiply string and string using *. only number * number or string *number is allowed
Example:
|
>>> “23” * “jai” TypeError: can't multiply sequence by non-int of type 'str' >>> 5 * “jai” jaijaijai >>> “23” * 34 232323232323232323 23232 |
>>>s1=123 >>>s2=456 >>>s3=”abc” >>>s4=”def” >>>print(s1+s2) 579 >>>s3+s3 abcabc |
>>>s1=15 >>>s3=”abc” >>>s1*s3 abc abcabcabcabc abc abcabcabcabc abcabcabcabcabc |
String Membership Operators: Membership Operators (in and not in) are used to check the presence of
character (s) any string.
- Membership Operators: in – Returns True if a character or a substring exists in the given string; otherwise False
- Membership Operators not in - Returns True if a character or a substring does not exist in the given string; otherwise, False
|
Example |
Output |
|
“a” in “python” |
False |
|
“a” in “java” |
True |
|
“per” in “operators” |
True |
|
“men” in “membership” |
False |
|
“Man” in “manipulation” |
False |
|
“Pre” not in “presence” |
True |
String Comparison Operators: These operators compare two strings character by character according to their ASCII value We can apply comparison operators (== ,!=, > ,<,>=,<=) on strings.
|
Characters |
Ordinal/ASCII code |
Example |
Output |
|
A-Z a-z 0-9 |
65 -90 97-122 48-57 |
str1=”program” str2=”python” str3=”Python” |
|
|
>>>ord(“a”) 97 |
>>>chr(97) a |
str1==str2 |
False |
|
>>>ord(“A”) 65 |
>>>chr(65) A |
str1!=str2 |
True |
|
str2==”python” |
True |
||
|
str2>str3 |
True |
||
|
str3<str1 |
True |
Finding the Ordinal or Unicode value of a character:
|
Function |
Description |
|
ord() |
Returns ordinal value of a character |
|
chr() |
Returns the corresponding character |
Q: Write a menu drive program to display ASCII code of a character and vice versa. Like Press-1 to find the ordinal value. Press-2 to find a character of a value and also ask do you want to continue Y/N
var=True
while (var):
choice=int(input("Press-1 to find the ordinal value \n Press-2 to find a character of a value\n"))
if choice==1:
ch=input("Enter a character : ")
print(ord(ch))
elif choice==2:
val=int(input("Enter an integer value: "))
print(chr(val))
else:
print("You entered wrong choice")
print("Do you want to continue? Y/N")
option=input()
if option=='y' or option=='Y':
var=True
else:
var=False
print("No i don't want to continue program