Class 11 Strings (Complete)
Class 11 Strings (Complete)
XI
Definition
A Python string is a sequence of characters stored in contiguous memory
locations.It is an immutable data type.
2
XI
Indexing
0 1 2 3 4 5
string P Y T H O N
-6 -5 -4 -3 -2 -1
Backward Indexing
3
XI
Traversing a string
Individual character of a string can be accessed using the method of indexing. Each
Traversing a string means accessing all the elements of the string one after
another by using the subscript or index where the index or subscript is given
within [ ] with the stringname.
The index used to access character of a string can be positive or negative integers.
Output
Amar Shah Lived in the King's Palace at Hyderabad
His best book was entitled as The Dream of "Doll" uncovered
6
XI
Assigning a string
String can also be created using Escape sequence characters as a part of the string.
Output
Programming Language: Python
Developed by Guido Van Rossum
7
XI
Assigning a string
Python does not support a character data type, these are treated as strings of length
A string without any character inside is known as Empty String. When an Empty string
is displayed using print(), a blank space gets displayed.
>>ch=""
>>print(ch)
''
8
XI
Operations on Strings
String
Concatenation Replication Comparison Membership
Slicing
9
XI
Concatenation
The + operator takes two strings as operands and results a new string by joining the
two operand strings.
Output
ComputerScience
10
XI
Replication
The * operator as a replication operator needs two operand i.e a string and an
Output
ComputerComputerComputer
11
XI
Comparison
All relational and comparison operators (==, >, <, <=, >=, !=) can be used to
compare two strings .
12
XI
Common Characters and Ordinal Values
CHARACTERS ORDINAL VALUES
Python provides a built in function ord() that takes in a single character and
returns the ordinal value.
Syntax : ord(<single-character>) ch=’a’
ord(ch)
>>ord("a")
97
13
XI
Membership
The operators in and not in are used to check whether a string exists in the given
14
XI
String Slicing
String slicing refers to the part of the string or a subset of a string.This subset of a
Example : string1[2:5]
C o m p u t e r
0 1 2 3 4 5 6 7
15
XI
String Slicing
However , while slicing string, start, end and step can be omitted.
16
XI
String Slicing
The start and end values can be –ve integers in which Python counts from
Example :
18
XI
String Slicing
0 1 2 3 4 5 6 7
Output
print(str[3:7] ) pute
print(str[4:] ) uter
print(str[:6] ) Comput
print(str[1::2] ) optr
print(str[-3:]) ter
19
XI
Answer the following :
String (str)
20
XI
Solution:
21
XI
String Functions & Methods
len()
lower()
upper()
isupper()
islower()
isdigit()
22
XI
String Functions & Methods
isspace()
rstrip()
lstrip()
partition()
replace()
split()
23
XI
String Functions and Methods
1. len()
Syntax : len(stringname)
Example
24
XI
String Functions and Methods
2. capitalize()
Syntax : stringname.capitalize()
Example
>>> str="hello"
>>>print(str.capitalize())
Hello
25
XI
String Functions and Methods
3. title()
Example
>>> str="hello how are you"
>>>print(str.title())
Hello How Are You
26
XI
String Functions and Methods
4. upper()
Syntax : stringname.upper()
Example
>>> str="learn python"
>>>print(str.upper())
LEARN PYTHON
27
XI
String Functions and Methods
5. lower()
Syntax : stringname.lower()
Example
>>> str="PYTHON"
>>>print(str.lower())
python
28
XI
String Functions and Methods
6. isupper()
Syntax : stringname.isupper()
Example
>>>word='ABC'
>>>print(word.isupper())
True
29
XI
String Functions and Methods
7. islower()
Syntax : stringname.islower()
Example
>>>word='xyz'
>>>print(word.islower())
True
30
XI
String Functions and Methods
8. isdigit()
Syntax : stringname.isdigit()
Example
>>>word='123'
>>>print(word.isdigit())
True
31
XI
String Functions and Methods
9. isalpha()
Syntax : stringname.isalpha()
Example
>>>word='alpha123'
>>>print(word.isalpha())
False
>>>word='abcdef'
>>>print(word.isalpha())
True
32
XI
String Functions and Methods
10. isalnum()
Syntax : stringname.isalnum()
Example
>>>word='alpha123'
>>>print(word.isalnum())
True
>>>word='abc123'
>>>print(word.isalnum())
True
33
XI
String Functions and Methods
11. isspace()
Syntax : stringname.isspace()
Example >>>word='alpha123'
>>>print(word.isspace())
False
>>>word=' '
>>>print(word.isspace())
True
34
XI
String Functions and Methods
12. split()
Syntax : stringname.split([separator,maxsplit])
Example
>>>word='DPS Mathura Road Delhi'
>>>print(word.split())
['DPS', 'Mathura', 'Road', 'Delhi']
35
XI
String Functions and Methods
>>>word='DPS,Mathura,Road,Delhi'
['DPS,Mathura,Road,Delhi']
>>>word='DPS,Mathura,Road,Delhi'
Example
>>>print(word.split(',',2)) Max splits
36
XI
Split function
#Splits at space
["LEARN", "PYTHON"]
print(T.split())
print(T.split('R'))
['LEA', 'N PYTHON']
37
XI
String Functions and Methods
13. strip()
Syntax : stringname.strip(character)
Syntax : stringname.lstrip(character)
39
XI
String Functions and Methods
15. rstrip()
Syntax : stringname.rstrip(character)
40
XI
String Functions and Methods
16. partition()
Syntax : stringname.partition(character)
41
XI
partition ()
42
XI
String Functions and Methods
17. find()
44
XI
String Functions and Methods
19. replace()
We Learn PYTHON
45
XI
Question 1
Program to traverse a string inputted by user and display it.
46
XI
Question 2
Program to input a string and display the string in reverse order.
47
XI
Question 3
Program to input a string and then print the number of uppercase letters, lowercase
49
XI
Question 5
Program to input a string and then check if it a palindrome or not.
50
XI
Question 6
Program that reads a string with multiple words and creates a new string which
53
XI
Question 8
Program that reads a string and a substring, and then display the number of
print(valid)
55
XI
Question 10
Write the equivalent code of upper() for a string. Accept a string. It should convert
56
XI
Question 11
Write a program to enter a string and form an integer by extracting all digits
57
XI
Question 12
Write a program to accept an identifier and check for its validity. An identifier is valid if it
is starting with a letter or underscore (_) followed by underscore (_ ) and any
59
XI
Solution
str=input("Enter a string-")
newstr=""
62
XI
Another solution:
63
XI
Programs
1. Write a program to input a string and a character and count the number of times a
character appears in the string.
65
XI
Solution :
A = "Passionate Programmer"
66
XI
Find the output :
s="ComPutEr"
67
XI
Solution:
s="ComPutEr"
68
XI
Find the output :
Name = "ComPUteR"
69
XI
Solution:
Name = "ComPUteR"
70
XI
Find the output :
Text1="AIsScE 2019"
2. word = 'CatBatSatFatOr'
for i in range(0, len(word), 3): # Splitting at 3
print(word[i:i+3])
3. txt = "apple#banana#cherry#orange"
x = txt.split("#", 1) # Splitting at ‘#’
print(x)
73
XI
Solution:
74
XI
© CS-DEPT DPS MATHURA ROAD
Happy Learning!!!
Thank you
75
XI