0% found this document useful (0 votes)
18 views46 pages

String in Python

Helps learn strings in python easily. Programming code fragments will help learn logic and syntax easier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views46 pages

String in Python

Helps learn strings in python easily. Programming code fragments will help learn logic and syntax easier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2025-11-02

String in Python
Understanding the concept of strings

1
2025-11-02

Click the emoji


to join our community

Alone we learn a lesson, together we build wisdom

Click the laptop


to get the latest version
of the PDF

2
2025-11-02

Click the link below


to access the videos and notes

Yo uTube Website

Alone we learn a lesson, together we build wisdom

ROHiT SiNGH 88 00 873 871

Quote

Learning new things will make you feel dumb,


when it’s actually making you smarter.

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

3
2025-11-02

This content covers the topics from these books and more..

4
2025-11-02

String in Python 88 00 873 871

What is a String

A string is a sequence of characters — which can include letters, digits, symbols, spaces, or special
characters — enclosed in single (' ') quotes , double (" ") quotes or triple (''' ''') quotes. Strings can be
single-line or multi-line, depending on how they are written.

Some examples of string


s1 = "123" s1 = 123 s1 = "123" s1 = 123
print(s1) print(s1) print(type(s1)) print(type(s1))
123 123 <class 'str'> <class 'int'>

© 2025 Rohit Singh. All rights reserved.

Designed with by Rohit Sir [Link]

ROHiT SiNGH 88 00 873 871

Some examples of string


s1 = "Hello" s2 = 'Hello' emp='' emp=str()
print(s1) print(s2) print(emp) print(emp)
Hello Hello ' ' ' '

s3 = '''Python s4='I'm fine' s4="I'm fine" s4='I\'m fine'


Rocks!''' print(s4) print(s4) print(s4)
print(s3) SyntaxError I'm fine I'm fine
Python
Rocks!

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

5
2025-11-02

String in Python 88 00 873 871

Some examples of string


name=str(1234) a='We are\n Indians'
print(name,type(name)) print(a)
1234 <class 'str'> We are
Indians

Formatting of a String

name = "Rohit"
subject = "CS"
print("Mr {} teaches {} ".format(name,subject))
Mr Rohit teaches CS

print("{1} love {0}".format("Python","We"))


We love Python
© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

ROHiT SiNGH 88 00 873 871

Traversing a string

Traversing a string means accessing each character of the string one by one, typically using a loop, to
perform operations such as searching, counting, or displaying characters. Traversing is like walking through
each letter of the string — step by step, from start to end — to examine or process it.

msg="superb" msg="Python" P
for i in msg: l=len(str) y
print(i,"-",end=' ') i=0 t
s -u -p -e -r -b while i<l: h
print(msg[i]) o
i+=1 n

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

6
2025-11-02

ROHiT SiNGH 88 00 873 871

Write a program to count the number of "p" in "pineapple". (including capital and small letters)

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

7
2025-11-02

String in Python 88 00 873 871

Write a program to count the number of "p" and "a" separately after getting the value from the user.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Write a program to count the number of occurrences of a given character entered by the user.

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

8
2025-11-02

String in Python 88 00 873 871

Write a program to count the number of vowels and consonants after getting the value from the user.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Write a program to count the number of times a character occurs and display its position too.

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

9
2025-11-02

String in Python 88 00 873 871

Write a program to read a string from the user and display each character along with its position (index).

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Basic Conceptual Questions

1) What is a string in Python?


2) How can a string be created in Python? Give examples using single, double, and triple quotes.
3) What is the difference between '123' and 123 in Python?
4) How can we create an empty string in Python? Write two different ways.
5) What does type() function return when used with a string and with an integer?
6) Why does s4='I'm fine' cause an error? How can you fix it?
7) What happens if you use both single and double quotes inside the same string incorrectly?
8) What is the difference between single-line and multi-line strings?

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

10
2025-11-02

ROHiT SiNGH 88 00 873 871

Updation in String

It is an immutable datatype, which means that the content of the string cannot be changed. It will internally
create a new string rather than modify the old string in place. A string is accessed using a subscript (index).

Example Think of a string like a necklace of beads. Each bead (character) has
s='anata won'
its fixed place on the thread — you can count or copy them, but
s[3]='y'
you can’t just replace one bead without making a new necklace.
print(s)
TypeError That’s why strings in Python are immutable.

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

11
2025-11-02

String in Python 88 00 873 871

Concatenation

It refers to the process of joining two or more strings together to form a single new string. This is achieved
using the + operator, which combines the contents of the strings in the same order as they are written.

A="India" n = "Rohit" n = "Rohit" A="India"


B="Gate" m = 89 m = 89 B="Gate"
C=A+B print(n + m) print(n+ str(m)) D=B+A
print(C) TypeError Rohit89 print(D)
IndiaGate GateIndia

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Replication

Replication means repeating a string multiple times to form a new string. In Python, this is done using the *
(asterisk) operator, which multiplies the string by a number (integer) — creating repeated copies of it.

a='Python' a='Hi' a='Hi'


print(a*3) print(a*3) print(a*'hoho')
PythonPythonPython HiHiHi TypeError

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

12
2025-11-02

String in Python 88 00 873 871

Membership operators

Membership operators are used to check whether a character or a substring exists within another string. Python
provides two membership operators — “in” and “not in” — to perform this check.
❑ in: returns True if the character or substring exists in the given string.
❑ not in: returns True if the character or substring does not exist in the given string

text = "good morning" text = "good morning" text = "good morning"


print("g" in text) print("M" in text) print("morning" in text)
True False True

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

ROHiT SiNGH 88 00 873 871

Comparison operators

In Python, strings can be compared using relational comparison operators such as <, >, <=, >=, ==, and !=.
Python compares strings lexicographically, meaning it compares the ASCII values of characters one by one
from left to right. 0 to 9 = 48-57 , A and Z = 65-90 , a and z = 97-122

Don't forget to ask your doubts in the comment box! [Link]

13
2025-11-02

String in Python 88 00 873 871

Comparison operators

Designed with by Rohit Sir [Link]

ROHiT SiNGH 88 00 873 871

Comparison operators
'a'<"A" "Goodbye" < "Hello"
False True

'ABC'>"AB" 'tie' == 'ties'


True False

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

14
2025-11-02

String in Python 88 00 873 871

Accessing Elements

We need to use an index that allows us to access an element of a list using either positive or negative
values. Slicing can also be done using an index in the form of [start, stop, step] to access the elements.

word='Hello world'

-ve Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


word H e l l o w o r l d
+ve Index 0 1 2 3 4 5 6 7 8 9 10

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

-ve Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


word H e l l o w o r l d
+ve Index 0 1 2 3 4 5 6 7 8 9 10

Indexing

print(L[5]) print(L[3]) print(L[5*0]) print(L[-2])

print(L[1:4]) print(L[5:]) print(L[:4]) print(L[:])

print(L[::2]) print(L[::3]) print(L[::-1]) print(L[3::2])

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

15
2025-11-02

ROHiT SiNGH 88 00 873 871

Write a program that accepts n times from the user and repeats 'GOOD MORNING' n number of times.

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

16
2025-11-02

String in Python 88 00 873 871

Write a program to ask the first, last and middle name of the user, then print the value in concatenation.

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

ROHiT SiNGH 88 00 873 871

Write a program that takes a string input and prints the characters at even indices only.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

17
2025-11-02

String in Python 88 00 873 871

Write a program that takes a string from the user and displays it in reverse without using loops.

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

ROHiT SiNGH 88 00 873 871

Predict the output

mysub="computer science" a = "Smart"


print(mysub[-7:-1]) b = "Brain"
print(mysub[::2]) print(a + b)
print(mysub[len(mysubt)-1])
print(mysub[:3] + mysub[3:]) x = "AI"
y = x * 3
print("abc" < "abd") print(y)
print("ZEBRA" > "apple")
name = "Rohit"
line = "good morning"
roll = 23
print(" " in line)
print(name + str(roll))
print("z" not in line)
© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

18
2025-11-02

Click the emoji


to join our community

Alone we learn a lesson, together we build wisdom

Click the laptop


to get the latest version
of the PDF

19
2025-11-02

Click the link below


to access the videos and notes

Yo uTube Website

Alone we learn a lesson, together we build wisdom

20
2025-11-02

ROHiT SiNGH 88 00 873 871

lower() method in strings upper() method in strings

The lower() method in Python is used to convert all The upper() method in Python is used to convert all
uppercase letters in a string to lowercase. Other lowercase letters in a string to uppercase. Other
characters (like digits, spaces, and symbols) remain characters (digits, spaces, symbols) remain unchanged.
unchanged.

Example Example
str='WELcome' str='WELcome'
print([Link]()) print([Link]())
welcome WELCOME

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

21
2025-11-02

String in Python 88 00 873 871

capitalize() method in strings title() method in strings

The capitalize() method in Python returns a new string The title() method returns a new string where the first
where the first character is converted to uppercase, and character of each word is capitalized, and all other
all other characters are converted to lowercase. characters are converted to lowercase.

Example Example
text = "python is powerful" text = "welcome to python programming"
line = [Link]() print([Link]())
print(line) Welcome To Python Programming
Python is powerful

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

ROHiT SiNGH 88 00 873 871

title() method in strings swapcase() method in strings

The title() method returns a new string where the first The swapcase() returns a new string where all uppercase
character of each word is capitalised, and all other letters are converted to lowercase, and all lowercase
characters are converted to lowercase. letters are converted to uppercase. Other characters,
such as digits, symbols, or spaces, remain unchanged.

Example Example
text = "welcome to python programming" text = "pyTHon"
line = [Link]() line = [Link]()
print(line) print(line)
Welcome To Python Programming PYthON

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

22
2025-11-02

String in Python 88 00 873 871

Predict the output


text = "coding IS love" text = "coding IS love"
print([Link]()) print([Link]())
print([Link]()) print([Link]())
print([Link]()) print([Link]())

text = "WELcome"
print([Link]())
print([Link]())

text = "welcome to python programming"


print([Link]())
print([Link]().upper())
© 2025 Rohit Singh. All rights reserved.

Designed with by Rohit Sir [Link]

ROHiT SiNGH 88 00 873 871

Write a program to accept a word from the user and count the number of upper and lower case.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

23
2025-11-02

24
2025-11-02

String in Python 88 00 873 871

len() method in strings

The len() function in Python is used to find the total number of characters in a string. This count includes
letters, digits, symbols, spaces, and special characters. If the length is 0, it is called an Empty String

Example Example
c='SinghClasses' text = "Python is fun!"
print(len(c)) print(len(text))
12 14

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

ROHiT SiNGH 88 00 873 871

index() method in strings

The index() method returns the position (index) of the first occurrence of a specified substring within the
given string. If the substring is not found, it raises a ValueError. Syntax: [Link](substring, start, end)

Example Example
text = "Welcome to Python" text = "banana"
pos = [Link]("Python") pos = [Link]("a", 2, 5)
print(pos) print(pos)
11 3

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

25
2025-11-02

String in Python 88 00 873 871

count() method in strings

The count() method returns the number of occurrences of a specified substring within a given string. It can also
be used to count occurrences within a specific range of indices using optional start and end parameters.

Example Example
text = "banana" text = "banana"
val=[Link]("a") val=[Link]("a",2,5)
print(val) print(val)
3 1

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

find() method in strings

The find() method in Python is used to search for a substring within a string and return the index (position)
of its first occurrence. If the substring is not found, it returns -1 instead of causing an error.

Syntax [Link](substring, start, end)

Example Example
str="jhony jhony yes papa" str="jhony jhony yes papa"
pos=[Link]('yes') pos=[Link]('jhony')
print(pos) print(pos)
12 0

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

26
2025-11-02

String in Python 88 00 873 871

find() method in strings


Example Example
text = "Learning Python is fun!" str="jhony jhony yes papa"
pos = [Link]("Python") pos=[Link]('jhony',2)
print(pos) print(pos)
9 6

Example
str="jhony jhony yes papa"
pos=[Link]('jhony',2,3)
print(pos)
-1

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

ROHiT SiNGH 88 00 873 871

replace() method in strings

The replace() method is used to replace a specified substring with another substring in a string. It creates a
new string with the replacements; the original string remains unchanged.

Example Example
str= "Honesty is the best policy" str='This is an example'
str=[Link](" ","-") print([Link]('is','was'))
print(str) Thwas was an example

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

27
2025-11-02

String in Python 88 00 873 871

Write a program to replace all the words 'do' with 'done' in the following string: "I do you do and we all will do"

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

ROHiT SiNGH 88 00 873 871

Write a program to check if the word is a palindrome or not

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

28
2025-11-02

29
2025-11-02

String in Python 88 00 873 871

strip() method in strings

The strip() method in Python is used to remove unwanted spaces or specified characters from both the
beginning and the end of a string. It’s often used to clean user input or text data.

Example Example
text = " Hello Python! " word = "***Welcome***"
str=[Link]() str=[Link]('*')
print(str) print(str)
Hello Python! [Link]('*')

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

ROHiT SiNGH 88 00 873 871

lstrip() method in strings

The lstrip() method removes spaces or specified characters only from the left side (beginning) of a string.

Example Example
text = " Hello Python! " word = "###Python###"
str=[Link]() str=[Link]('#')
print(str) print(str)
Hello Python! Python###

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

30
2025-11-02

String in Python 88 00 873 871

rstrip() method in strings

The rstrip() method removes spaces or specified characters only from the right side (ending) of a string.

Example Example
text = " Hello Python! " word = "***Welcome***"
str=[Link]() str=[Link]('*')
print(str) print(str)
Hello Python! ***Welcome

© 2025 Rohit Singh. All rights reserved.

Designed with by Rohit Sir [Link]

ROHiT SiNGH 88 00 873 871

replace() method in strings

The replace() method is used to replace a specified substring with another substring in a string. It creates a
new string with the replacements; the original string remains unchanged.

Example Example
str= "Honesty is the best policy" str='This is an example'
str=[Link](" ","-") print([Link]('is','was'))
print(str) Thwas was an example

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

31
2025-11-02

String in Python 88 00 873 871

split() method in strings

The split() method divides a string into a list of substrings, based on a specified separator (delimiter). If no
separator is provided, it uses whitespace (space, tab, newline) by default.
Syntax: [Link](separator, maxsplit)

Example Example
text = "Python is powerful" line = "one two three four"
words = [Link]() words = [Link](' ', 2)
print(words) print(words)
['Python', 'is', 'powerful'] ['one', 'two', 'three four']

© 2025 Rohit Singh. All rights reserved.

Designed with by Rohit Sir [Link]

ROHiT SiNGH 88 00 873 871

partition() method in strings

The partition() method searches for a specified separator in the string. It returns a tuple containing these
three elements and divides the string into three parts: The part before the separator, the separator itself,
and the part after the separator.

Example Example
text = "Python is powerful" msg = "Learning never ends"
print([Link]("is")) print([Link]("AI"))
('Python ', 'is', ' powerful') ('Learning never ends', '', '')

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

32
2025-11-02

ROHiT SiNGH 88 00 873 871

Write a program to input a line of text from the user and print the longest word in the line.

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

33
2025-11-02

String in Python 88 00 873 871

Write a program that replaces all vowels in the string with “*” entered by the user.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Write a program to accept a string and display each word and its length.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

34
2025-11-02

String in Python 88 00 873 871

Write a program to input a string, then count and display the occurrences of words starting with I and E.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Write a program to input a string, then count and display the occurrences of words starting with a vowel.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

35
2025-11-02

String in Python 88 00 873 871

join() method in strings

The join() method combines the elements of an iterable (like a list, tuple, or string) into a single string,
inserting the calling string as a separator between each element. Syntax [Link](iterable)

Example Example
numbers = ('1', '2', '3', '4') items = ['apple', 'banana', 'cherry']
print('-'.join(numbers)) print(', '.join(items))
1-2-3-4 apple, banana, cherry

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

startswith() method in strings

The startswith() method checks whether a string starts with a specified prefix (substring). It returns True if
the string begins with the given substring; otherwise, False. Syntax [Link](substr,start, end)

Example Example
text = "Python Programming" msg = "welcome to python"
print([Link]("Py")) print([Link]("come", 3, 8))
True True

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

36
2025-11-02

String in Python 88 00 873 871

endswith() method in strings

The endswith() method checks whether a string ends with a specified suffix (substring). It returns True if
the string ends with that substring; otherwise, False. Syntax [Link](substring, start, end)

Example Example
quote = "Learning is continuous" msg = "Hello Python"
print([Link]("continuous")) print([Link]("on", 4, 12))
True True

© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

ROHiT SiNGH 88 00 873 871

endswith() method in strings

The ord() function returns the Unicode (ASCII) integer value of a given character. Syntax: ord(character)
The chr() function does the reverse of ord(). It returns the character that corresponds to a given Unicode
(ASCII) integer. Syntax chr(integer)

Example Example
print(ord('A')) print(chr(65))
print(ord('a')) print(chr(97))
print(ord(' ')) print(chr(36))
65 A
97 a
32 $
© 2025 Rohit Singh. All rights reserved.

Don’t forget to subscribe | Stay updated! [Link]

37
2025-11-02

String in Python 88 00 873 871

Predict the output


line = " Python is fun! " address="waz-1, new ganga nagar, new delhi"
print([Link]()) ❑ print([Link]())
print([Link]()) ❑ print([Link]())
print('-'.join(['Code','is','Art’])) ❑ print([Link]("new"))
❑ print ([Link]("new"))
msg = " The future is AI " ❑ print ([Link](" "))
data = [Link]() ❑ print ([Link](","))
print([Link]("The")) ❑ print ([Link]("new","old"))
print([Link]("AI")) ❑ print (address. index("agar"))
print([Link]("AI")) ❑ print (address. index("agra"))
print([Link]("AI"))

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Write the appropriate statement for the following task

str='Hello World'. ❑ To check whether the string contains digits.


❑ Can you add two strings(Y/N)? ❑ To convert the first letter of a string to uppercase.
❑ Find the occurrence of a string. ❑ To convert all the letters of a string to upper case.
❑ Capitalise all the letters of the string. ❑ Find the occurrence of a string within another string.
❑ Remove all white spaces from the string. ❑ Remove all the white spaces from the beginning.
❑ Check whether the string contains digits. ❑ Check whether all the letters of the string are in
❑ Check whether the letters are in capital. capital letters.
❑ Convert the first letter of a string to upper case.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

38
2025-11-02

String in Python 88 00 873 871

Consider the string str=”Global Warming” Write the appropriate statement for the following

Write statements to implement the following ❑ To trim the characters from the string.
❑ To display the last four characters. ❑ Count the number of characters in the string
❑ To display a string starting from 4 to 8. ❑ Change the first character to a capital letter
❑ To change the case of the given string. ❑ To change a lowercase letter to an uppercase letter
❑ To check if the string is in title case. ❑ Check whether the given character is a letter.
❑ To replace all the occurrences 'a' with '*’ ❑ Replace all the occurrences of the old string with the
❑ To check whether a string is alphanumeric or not. new string.
❑ To display the starting index for the substring „Wa‟.

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

39
2025-11-02

ROHiT SiNGH 88 00 873 871

isalnum() method in strings

The isalnum() method checks whether all characters in a string are alphanumeric, i.e., letters (A–Z, a–z) and numbers
(0–9). It returns:

True → if every character is a letter or digit and the string is not empty.

False → if there is any space, symbol, or punctuation.

text = "Python3" word = "Python 3" word = "Hello!"


print([Link]()) print([Link]()) print([Link]())
True False False

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

40
2025-11-02

String in Python 88 00 873 871

isalpha() method in strings

The isalpha() method in Python is used to check whether all characters in a string are alphabetic letters (A–Z, a–z). It
returns True if the string contains only letters and is not empty; otherwise, it returns False. This is useful when
validating names, words, or any input that should contain letters only, without digits, spaces, or special characters.

text1 = "Python" text2 = "Python3" text3 = "Python Program"


print([Link]()) print([Link]()) print([Link]())
True True False

© 2025 Rohit Singh. All rights reserved.

Designed with by Rohit Sir [Link]

ROHiT SiNGH 88 00 873 871

isdigit() method in strings

The isdigit() method in Python is used to check whether all characters in a string are numeric digits (0–9). It
returns True if the string contains only digits and is not empty; otherwise, it returns False. This is
particularly useful when validating numbers, IDs, PINs, or any input that should consist solely of digits.

num1 = "12345" num2 = "123a5" num3 = "123 45"


print([Link]()) print([Link]()) print([Link]())
True False False

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

41
2025-11-02

String in Python 88 00 873 871

islower() method in strings

The islower() method in Python checks whether all alphabetic characters in a string are in lowercase (a–z).
It returns True if all letters are lowercase and there is at least one alphabetic character; otherwise, it returns
False. This method is useful when validating text input that requires lowercase letters.

text1 = "python" text2 = "Python" text3 = "python123"


print([Link]()) print([Link]()) print([Link]())
True False True

© 2025 Rohit Singh. All rights reserved.

Go Slow, doubling the speed will not double the speed of understanding [Link]

ROHiT SiNGH 88 00 873 871

isupper() method in strings

The isupper() method in Python checks whether all alphabetic characters in a string are in uppercase (A–Z).
It returns True if all letters are uppercase and there is at least one alphabetic character; otherwise, it
returns False. This method is useful for validating input where uppercase letters are required.

text1 = "PYTHON" text2 = "Python" text4 = "12345"


print([Link]()) print([Link]()) print([Link]())
True False False

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

42
2025-11-02

String in Python 88 00 873 871

isspace() method in strings

The isspace() method in Python checks whether all characters in a string are whitespace characters, such as spaces,
tabs (\t), or newlines (\n). It returns True if the string contains only whitespace and is not empty; otherwise, it returns
False. This method is useful when validating input to detect blank entries or to clean unnecessary spaces.

text1 = " " text2 = " \t\n " text3 = "Python "
print([Link]()) print([Link]()) print([Link]())
True True False

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

istitle() method in strings

The istitle() method in Python checks whether each word in a string starts with an uppercase letter followed by
lowercase letters. It returns True if the string follows the title case format and is not empty; otherwise, it returns False.
This method is useful when validating names, book titles, or headings.

Example Example
text1 = "Python Programming" text2 = "python Programming"
print([Link]()) print([Link]())
True False

© 2025 Rohit Singh. All rights reserved.

CS IP BCA MCA classes by Rohit Sir [Link]

43
2025-11-02

String in Python 88 00 873 871

Write a program that accepts any string and counts the number of alphabets & digits present in it.

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

ROHiT SiNGH 88 00 873 871

Read a string from user and capitalise every alternate letter in the string, eg, PaSsIoN becomes pAsSiOn.

© 2025 Rohit Singh. All rights reserved.

Don't forget to ask your doubts in the comment box! [Link]

44
2025-11-02

String in Python 88 00 873 871

Predict the output


s1 = " " text = "Python3"
s2 = "\t\n " print([Link]() and [Link]())
s3 = "Python Programming" print([Link]() or [Link]())
s4 = "python programming"
print([Link](), [Link]()) msg = "HELLO world"
print([Link](), [Link]()) print([Link](), [Link]().isupper())
print("Welcome To INDIA".istitle()) print(" ".isspace(), " ".isspace())

© 2025 Rohit Singh. All rights reserved.

[Link] Join us on WhatsApp & Telegram for regular updates

Thank You
For Watching!
Please share your suggestions related to the
video in the comments.

45
2025-11-02

Subscribe Now
And be a part of our Family
Please share your suggestions related to the
video in the comments.

46

You might also like