Python Programming – Special String Operators

You can learn about Strings in Python Programs with Outputs helped you to understand the language better.

Python Programming – Special String Operators

Strings can be manipulated using operators like concatenation)*), repetition)*) and membership operators like m and not in. Different special string operators are discussed in the following sections:

Concatenation (+) Operator

The + operator is known as the concatenation operator used to join the strings given on either side of the operator. For example,
>>> ‘Work ‘ + ‘Hard’
‘Work Hard’

To give a white space between the two words, insert a space before the closing of the first string within quotes.
>>> ‘Work ‘ + ‘Hard’
‘Work Hard’
Note that the interpreter gives back the string with single quotes, regardless of whether you enter strings with single quotes or double-quotes. The + operator can work with numbers and strings separately for addition and concatenation, respectively. However, you cannot combine numbers and strings as operands with a + operator. It will produce an error.
>>> 7 + 7
14 # (valid) + operator is used as addition
>>>’7′ + ‘7’ # (valid) + operator is used as concatenation
’77’
>>> 7 + ‘7’# (invalid) it will produce an error
You cannot concatenate integers and strings together.

The + operator must have both operands of the same type. It cannot work with one operand as a number and the other a string.

Replication (*) Operator

There may be times when you need to use Python to automate tasks, and one way you may do this is by repeating a string several times. Replication * (multiply) gives the multiplication of the two numbers or returns the string repeated that many times. For example,
>>> 5*7
35
>>> “Save!” * 3
‘Save!Save!Save!’

When the * (multiplication) operator works with numbers, it returns the product of two numbers. The (replication) operator used with string produces multiple copies of the same string. For replication, if one of the operands is a string, the other has to be a number, i.e., “string” * number or number * “string”. It cannot work with both operands of string types. It will display an error.
>>> 7*7
4 9 # (valid) * operator is used as mul-tiplication
>>> ‘Stop ‘ * 2
‘Stop Stop’ # (valid) * operator is used as replication >» 5 * ‘$’
‘$$$$$’ # (valid) * operator is used as replication
>>> ‘2’ * ‘5’# (invalid) it will produce an error

  • Strings can include any characters, including digits. Quotes turn digits into strings.
  • You can concatenate strings with the “+” operator and create multiple concatenated copies of the string with the operator.

Recommended Reading On: C Programming Examples with Output

Membership Operator (in)

Membership operator in returns true if a character exists in the given string, false otherwise. For example,
>>> str = “Work Hard”
>>> ‘W’ in str
True
>>> ‘Work’ in str
True
>>> ‘work’ in str
False # because w of work is in small case
>>> ‘We’ in str
False

Let’s Try

Write the output of the following code segments:

print (‘black’+ ‘white’) print(“Hello” * 3)
print ((‘h’) in ‘elephant’) print ((‘i’) in ‘elephant’)
Comparisons Result Reason
“abc” == “abc” True Because letter case is same.
“a” == “A” False Because letter case is different.
“a” != “A” True Because letter case is different.
“abc” > “ABC” True Because uppercase letters are considered smaller than the lower­case letters due to their ASCII values.
‘123’ > ‘456’ False Because the ASCII value of 123 is lower than 456.

Membership Operator (not in)

The not in membership operator returns true if a character does not exist in the given string. For example,

>>> str = “Work Hard”
>>> ‘W’ not in str
False
>>> ‘Word’ not in str
True

  • The in and not in are membership operators that test for the presence of a substring in the string.

Comparison Operators

All the comparison operators (<, <=, >, >=, !=, ==) are applied to strings also. The comparisons using these operators follow the standard character-by-character comparison rules for ASCII and Unicode. ASCII values of numbers 0 to 9 are from 48 to 57, uppercase (A to Z) is from 65 to 90, and lowercase (a to z) is from 97 to 122. Python has two bool¬ean values, True and False. Many comparisons

operators return True and False as depicted in Figure 6.14.
At the most basic level, computers store all information as numbers. To represent character data, a translation scheme is used which maps each character to its representative number.
Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This extension allows

the strings to include characters from the different languages of the world. Python 3 supports Unicode extensively, including allowing Unicode characters within strings. Python provides a set of built-in functions that allows you to switch back and forth between characters and their corresponding ASCII and Unicode values. The ord( ) function returns the numeric (“ordinal”) code of a single-character string, while Chr( ) is the inverse of ord( ). Chr( ) returns a single character string referring to the given numeric code. (See Figure 6.15). Here are also some interactive examples:
>>> ord(“a”) # ord( ) function will return numeric values
97
>>> ord(“A”)
65
>>> ord(‘#’)
35
>>> Chr(97) # chr() returns an integer to a character
‘a’
>>> Chr(90)
‘ Z’

  • ord( ) function returns the ASCII or Unicode value of a character and requires a single character only.
  • Chr( ) function takes ASCII or Unicode value in integer form and returns the character corresponding to the ASCII or Unicode value.

Python Programming - Special String Operators chapter 6 img 1

Leave a Reply

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