String Membership Test
We can test if a sub string exists within a string or not, using the keyword in and not in.
In operator
Example:
n="computer" print("o" in n) print("t" in n) print("e" in n) print("w" in n) print("v" in n) print("z" in n)
Output:
True
True
True
False
False
False
>>>
Example:
n="Hello World" z="o" in n print("z = ",z) z="w" in n print("z = ",z) z="d" in n print("z = ",z)
Output:
z = True
z = False
z = True
>>>
not In operator
Example:
n="computer" print("o" not in n) print("t" not in n) print("e" not in n) print("w" not in n) print("v" not in n) print("z" not in n)
Output:
False
False
False
True
True
True
>>>
Python String Formatting
Escape Sequence
If we want to print a text like:
He said, “What’s there?”
we can neither use single quote or double quotes. This will result into SyntaxError as the text itself contains both single and double quotes.
>>> print(“He said, “What’s there?””)
…
SyntaxError: invalid syntax
>>> print(‘He said, “What’s there?”‘)
…
SyntaxError: invalid syntax
One way to get around this problem is to use triple quotes. Alternatively, we can use escape sequences.
An escape sequence starts with a backslash and is interpreted differently. If we use single quote to represent a string, all the single quotes inside the string must be escaped.
Similar is the case with double-quotes. Here is how it can be done to represent the above text.
#single quote is allowed inside double quote
print(“single quote are allowed inside double quote”)
n=”hello how are you”
print(n)
n=”hello ‘how’ are you”
print(n)
n=”hello ‘how’ are ‘you'”
print(n)
output:
single-quote are allowed inside double quote
hello how are you
hello ‘how’ are you
hello ‘how’ are ‘you’
#double quote is allowed inside single quotes
print(“double quote are allowed inside single quote”)
n=’hello how are you’
print(n)
n=’hello “how” are you’
print(n)
n=’hello “how” are “you”‘
print(n)
output
double-quote are allowed inside single quote
hello how are you
hello “how” are you
hello “how” are “you”
To use a single quote within single quote
Example:
n=’hello how are you’
print(n)
#want output as : hello ‘how’ are you
n=’hello \’how\’ are you’
print(n)
#want output as : hello ‘how’ are ‘you’
n=’hello \’how\’ are \’you\”
print(n)
output
hello how are you
hello ‘how’ are you
hello ‘how’ are ‘you’
To use double quote within double quote
example:
n=”hello how are you”
print(n)
#want out put as : hello “how” are you
n=”hello \”how\” are you”
print(n)
#want out put as : hello “how” are “you”
n=”hello \”how\” are \”you\””
print(n)
hello how are you
hello “how” are you
hello “how” are “you”
# using triple quotes
print(”’He said, “What’s there?””’)
# escaping single quotes
print(‘He said, “What\’s there?”‘)
# escaping double quotes
print(“He said, \”What’s there?\””)
output:
He said, “What’s there?”
He said, “What’s there?”
He said, “What’s there?”
The format() Method for Formatting Strings
The format() method that is available with the string object is very versatile and powerful in formatting strings. Format strings contain curly braces {} as placeholders or replacement fields which get replaced.
We can use positional arguments or keyword arguments to specify the order.
# default(implicit) order
n=”{}, {} and {}”.format(‘Amit’,’Sumit’,’Harsh’)
print(n)
Amit, Sumit and Harsh
# order using positional argument n="{0}, {1} and {2}".format('Amit','Sumit','Harsh') print(n)
Output:
Amit, Sumit and Harsh
>>>
# order using positional argument
n=”{0}, {1} and {2}”.format(‘Amit’,’Sumit’,’Harsh’)
print(n)
Amit, Sumit and Harsh
# order using positional argument n="{0}, {1} and {2}".format('Amit','Sumit','Harsh') print(n)
Output:
Amit, Sumit and Harsh
# order using positional argument
n=”{1}, {0} and {2}”.format(‘Amit’,’Sumit’,’Harsh’)
print(n)
Sumit, Amit and Harsh
# order using positional argument n="{1}, {0} and {2}".format('Amit','Sumit','Harsh') print(n)
Output:
Sumit, Amit and Harsh
# order using positional argument
n=”{2}, {0} and {1}”.format(‘Amit’,’Sumit’,’Harsh’)
print(n)
Harsh, Amit and Sumit
# order using positional argument n="{2}, {0} and {1}".format('Amit','Sumit','Harsh') print(n)
Output:
Harsh, Amit and Sumit
# order using keyword argument
n=”{a}, {s} and {h}”.format(a=’Amit’,s=’Sumit’,h=’Harsh’)
print(n)
Amit, Sumit and Harsh
# order using keyword argument n="{a}, {s} and {h}".format(a='Amit',s='Sumit',h='Harsh') print(n)
Output:
Amit, Sumit and Harsh
# order using keyword argument
n=”{h}, {s} and {a}”.format(a=’Amit’,s=’Sumit’,h=’Harsh’)
print(n)
Harsh, Sumit and Amit
# order using keyword argument n="{h}, {s} and {a}".format(a='Amit',s='Sumit',h='Harsh') print(n)
Output:
Harsh, Sumit and Amit