PS Unit 3
PS Unit 3
Y-2020-2021
FYBCA-SEM2-204 - Programming Skills
UNIT-3: Python Strings and Operators
3.1 Python Strings:
Python string is the collection of the characters surrounded by single quotes, double
quotes, or triple quotes. The computer does not understand the characters; internally, it
stores manipulated character as the combination of the 0's and 1's. Each character is
encoded in the ASCII or Unicode character. So we can say that Python strings are also
called the collection of Unicode characters.
In Python, strings can be created by enclosing the character or the sequence of
characters in the quotes. Python allows us to use single quotes, double quotes, or triple
quotes to create the string.
Consider the following example in Python to create a string.
str1 = "Hi Python !"
print(type(str1))
In Python, strings are treated as the sequence of characters, which means that Python
doesn't support the character data-type; instead, a single character written as 'p' is
treated as the string of length 1.
As shown in Python, the slice operator [] is used to access the individual characters of
the string. However, we can use the : (colon) operator in Python to access the substring
from the given string. Consider the following example.
We can do the negative slicing in the string; it starts from the rightmost character, which
is indicated as -1. The second rightmost index indicates -2, and so on. Consider the
following image.
However, in example 1, the string str can be assigned completely to a new content as
specified in the following example.
str = "HELLO"
print(str)
str = "hello"
print(str)
OUTPUT:
HELLO
hello
String Operators
Operator Description
+ It is known as concatenation operator used to join the strings given either
side of the operator.
* It is known as repetition operator. It concatenates the multiple copies of the
same string.
[] It is known as slice operator. It is used to access the sub-strings of a
particular string.
[:] It is known as range slice operator. It is used to access the characters from
the specified range.
in It is known as membership operator. It returns if a particular sub-string is
present in the specified string.
not in It is also a membership operator and does the exact reverse of in. It returns
true if a particular substring is not present in the specified string.
r/R It is used to specify the raw string. Raw strings are used in the cases where
we need to print the actual meaning of escape characters such as
"C://python". To define any string as a raw string, the character r or R is
followed by the string.
% It is used to perform string formatting. It makes use of the format specifiers
used in C programming like %d or %f to map their values in python. We will
discuss how formatting is done in python.
Escape Sequence
Let's suppose we need to write the text as - They said, "Hello what's going on?"- the
given statement can be written in single quotes or double quotes but it will raise the
SyntaxError as it contains both single and double-quotes.
Consider the following example to understand the real use of Python operators.
str = "They said, "Hello what's going on?""
print(str)
Output:
SyntaxError: invalid syntax
We can use the triple quotes to accomplish this problem but Python provides the escape
sequence.
The backslash(/) symbol denotes the escape sequence. The backslash can be followed by
a special character and it interpreted differently. The single quotes inside the string must
be escaped. We can apply the same as in the double quotes.
Example
# using triple quotes
print('''They said, "What's there?"''')
3.1.3 String Methods: (center, count, join, len, max, min, replace, lower, upper,
replace, split)
No Method Description
1 format(value) It returns a formatted version of S, using the passed
value.
Eg: #named indexes:
txt1 = "My name is {fname}, I'm
{age}".format(fname = "John", age = 36)
#numbered indexes:
txt2 = "My name is {0}, I'm {1}".format("John",36)
#empty placeholders:
txt3 = "My name is {}, I'm {}".format("John",36)
print(txt1)
print(txt2)
print(txt3)
2 center(width ,fillchar) It returns a space padded string with the original
string centred with equal number of left and right
spaces.
Eg: str = "Hello Javatpoint"
# Calling function
str2 = str.center(20,'#')
# Displaying result
print("Old value:", str)
print("New value:", str2)
3 string.count(value, start, It returns the number of times a specified value
end) appears in the string.
value: Required. A String. The string to value to
search for
start: Optional. An Integer. The position to start the
search. Default is 0
end: Optional. An Integer. The position to end the
search. Default is the end of the string
Eg1: txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Eg2: txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)
4 join(seq) It merges the strings representation of the given
sequence.
Eg : str = "->" # string
list = {'Java','C#','Python'} # iterable
str2 = str.join(list)
print(str2)
5 len(string) It returns the length of a string.
Eg: x = len("Hello")
print(x)
3.2.5 Identity and member operators (is, is not, in, not in)
Membership Operators
Python membership operators are used to check the membership of value inside a
Python data structure. If the value is present in the data structure, then the resulting
value is true otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the second operand
(list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second
operand (list, tuple, or dictionary).
Identity Operators
The identity operators are used to decide whether an element certain class or type.
Operator Description
is It is evaluated to be true if the reference present at both sides point to the
same object.
is not It is evaluated to be true if the reference present at both sides do not point
to the same object.
Operator Description
exponent operator is given priority over all the others
**
used in the expression.
~+- negation, unary plus, and minus.
multiplication, divide, modules, reminder, and floor
* / % //
division.
+- Binary plus, and minus
>> << Left shift. and right shift
& Binary and(Bitwise)
^| Binary xor, and or (Bitwise)
Comparison operators (less than, less than equal to,
<= < > >=
greater than, greater then equal to).
<> == != Equality operators.
= %= /=
//= -=
Assignment operators
+=
*= **=
is is not Identity operators
in not in Membership operators
not or and Logical operators