Strings in Python
Strings in Python
CLASS 11
CBSE
What are Strings?
0 1 2 3 4 5 6 7 8 9 10 11
H E L L O W O R L D !
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
> If there are both single and double quotes in the string, we need to escape
the quotes using \ character in front of them to indicate the special nature
of the character.
Creating Strings
*If the statement is very long, then it can be shifted to multiple lines with the line continuation
character ( \ ), but it shows the result in the same line
0 1 2 3 4 5 6 7 8 9 10 11
H E L L O W O R L D !
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7
m y w o r l d
-8 -7 -6 -5 -4 -3 -2 -1
Accessing / Iterating each index of a string using either for or while loop
a) Iterating through string using for loop
#output #backend
S1 = “my world”
m Print(index0)
For i in S1: y Print(1)
Print(i) Print(2) space
w Print(i)
o Print(i)
r Print(6)
l Print(7)
d
Traversing a String #output
m
y
0 1 2 3 4 5 6 7 w
o
m y w o r l d
r
-8 -7 -6 -5 -4 -3 -2 -1 l
d
b) Iterating through string using while loop
Loop runs till the condition index<len(S1) is true, #backend
Where index increments from 0 to len(S1) - 1 0<8 S1[0]
1<8 S1[1]
S1 = “my world” 2<8 S1[2]
3<8 S1[3]
index = 0 …………….
While index < len(S1): #index < 8 7<8 S1[7]
8<8 end
print(S1[index], end = “\n”)
index += 1 #index = index +1
Special String Operations
Operations Details Example
1) Concatenation String1 + String2 „Good ‟ + „Morning‟
• Adding two strings Good Morning #output
• Adding an integer value to a string leads to „Good „ + 2 #Error
TypeError „6‟ + „3‟ #63 output
Example Description
>>>A=„SAVE MONEY‟ Start from 1
>>>A[1:3] End at 2 i.e. (3-1)
„AV‟
>>>A[ : 3] If start is missing, default start is 0
„SAV‟
>>>A[3: ] If end is missing, it will to till last index
„E MONEY‟
>>>A[ : ] Entire string
„SAVE MONEY‟
>>>A[-2 : ] Irrespective of the starting point, it will print from left to
„EY‟ right
>>>A[ : -2] End index will be -2-1 = -3
„SAVE MON‟
>>>A[ : :2] Started from index 0, with stepping of 2
„SV OE‟
String Methods and Built-In Functions
Print(s1.lstrip(eL))
arn Python #output
String Methods and Built-In Functions
Function Meaning Syntax Example
rstrip() • Returns the string after Str.rstrip() >>>S1=„ Learn Python ‟
removing spaces from the Str.rstrip(chars) Print(s1.rstrip())
r stands for right Learn Python #output
right • chars specify the set of
characters to be removed >>>S1=„ Learn Python‟
from the right, all the Print(s1.rstrip(on))
combinations of chars are Learn Pyth #output
removed
Print(s1.rstrip(no))
Learn Pyth #output
strip() Returns the string after removing Str.strip() >>>S1=„ Learn Python ‟
the spaces from both left and Print(s1.strip())
right Learn Python #output
print(colors.split(„ : „, 0))
[„Red:Blue:Orange:Pink‟ ]
String Methods and Built-In Functions
Conditionals in Python
* if
* if-else
* if-elif
* nested if
* storing conditions
Statement Flow Control
● Equals: a == b
● Not Equals: a != b
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
If
Syntax Flow Example
if age = 16
condition: limit = 25
if (age == 16):
statements print('Age is correct as
mentioned')
(s)
if (age < 25):
print('Below age')
if (age > 10):
print('Teen')
If…else
Loop - Set of statements which is executed repeatedly, until the end condition is
satisfied.
Both conditional and loop are compound statements (a group of statements executed as a unit).
Represented by colon :
Looping Statements - for , range(), while, loop else, nested loops
#the control variable takes a different value on each iteration in the sequence.
For example ,
#output 15 25 35 45
Statement Flow Control
range() based for loop
For number based list, range() function can be used. Below are 3 ways to define a
range:
range(stop) #elements from 0 to stop-1, increment by 1
range(start, stop) #elements from start to stop-1, increment by 1
range(start, stop, step) #elements from start to stop-1, increment by step
For example:
range(5) > 0, 1, 2, 3, 4 for sq in range(3, 6) :
print(“Square of no. ” , sq , “is ”,
range(2, 10) > 2, 3, 4, 5, 6, 7, 8, 9 (sq**2)) #3^2 = 3*3
range(2, 10, 3) > 2, 5, 8
#
range(10, 2) > no value, start can‟t be sq = 3
greater than stop unless it is a decreasing seq Square of no. 3 in 9 #end of 1st
range(10, 2, -1) > 10, 9, 8, 7, 6, 5, 4, 3 loop
Sq = 4
Square of no. 4 in 16 #end of
#a program to print squares of numbers in the
2nd loop
range of 3 to 5 Sq = 5
for sq in range(3, 5) : Square of no. 5 in 25 #end of
print(“Square of no. ” , sq , “is ”, (sq**2)) 3rd loop
Statement Flow Control
While loop
Conditional loop that will repeat the instructions as long as a conditional remains true
(boolean True). When the expression becomes false, the program control passes to the
line after the loop-body.
The while loop is used when it is not possible to know in advance how many times
the loop will be executed, but the termination condition is known. It is an entry-
controlled loop.
For example:
#program to calculate the sum of numbers until the user enters zero
total = 0
number = int(input('Enter a number: ')) Sum of numbers => variable
#add numbers until number is zero => total
while number != 0: Number => 2 => total =
total += number # total = total + number number
#take integer input again 3 => total = total+number =
number = int(input('Enter a number: ')) 5
10 => 5+15
print('total =', total)