4.7.Python String
4.7.Python String
STRING IN PYTHON
Basics of String:
Basic String Operators
a) String Concatenation Operator (+)
b) String Replication Operator (*)
C) Membership Operators: (in & not in)
D) ord() & chr() functions
F) String Slice :
for loop with string
Built in functions in String
STRING IN PYTHON
Definition: String is a collection of characters. Strings can be created by enclosing characters
inside a single quote or double-quotes. Even triple quotes can be used in Python but
generally used to represent multiline strings. Python does not have a character data type, a
single character is simply a string with a length of 1.
Basics of String:
Strings are immutable means that the contents of the string cannot be changed after it is
created. At the same memory address, the new value cannot be stored. Python does not
allow the programmer to change a character in a string.
Example:
str=‘jaipur’
As shown in the above example.As shown in the above example, str has the value “jaipur”.
An attempt to replace ‘j’ in the string by ‟J‟ displays a TypeError.
Each character has its index or can be accessed using its index. String in python has two-way
index for each location. (0, 1, 2, ……. In the forward direction and -1, -2, -3,……… in the
backward direction.)
Important points about accessing elements in the strings using subscript(INDEXING) Positive
subscript helps in accessing the string from the beginning Negative subscript helps in
accessing the string from the end. Subscript 0 or –ve n(where n is length of the string)
displays the first element. Example : A[0] or A[-5] will display “H” Subscript 1 or –ve (n-1)
displays the second element.
“Hello” + “Students”
“a” + 5 is invalid
The ‘*’ operator when used with String, then it creates a new string that is a number of
replications of the input string. e.g.
in : returns True if a character or a substring exists in the given string, False otherwise.
“a” in “Rajat” gives: True “Raj” in “Rajat” gives: True “67” in “1234” gives: False
not in: returns True if a character or a substring does not exists in the given string, False
otherwise. “a” not in “Rajat” gives: False "Raj“ not in “Rajat” gives: False “67” not in “1234”
gives: True
ord() function: It gives ASCII value of a single character ord (“a”) : 97 ord (“Z”) : 90 ord(“0”) :
48 chr() function: it is opposite of ord() function chr(97) : “a" chr(90) : “Z" chr(48) : “0”
F) String Slice :
Extracting a subpart from a main string is called slicing .It is done by using a range of indices.
Syntax:
string-name[start:stop:step]
Note: it will return a string from the index start to stop-1. Example:
s=“TEACHER”
0
-7
-6
-5
-4
-3
-2
-1
s[2:6:1] ‘ACHE’
s[6:1:-1] ‘REHCA’
s[0:10:2] ‘TAHR’
s[-8:-3:1] ’TEAC
s[ : :-1] ‘REHCAET’
s[2: :]+s[ :2 :] ‘ACHERTE’
s[1: 5:-1]‘ ‘
Let word=“WelCome” Then, word [0:7] will give : WelCome word [0:3] will give : Wel word
[2:5] will give : lCo (2,3,4) word [-6:-3] will give : elC (-6,-5,-4) word [ :5] will give : WelCo
(0,1,2,3,4) word [3: ] will give : Come (5,6,7,8)
e.g.1:
for ch in “Hello” :
print(ch)
Output: H e l l o