0% found this document useful (0 votes)
4 views4 pages

Class8&9 String Methods

The document provides examples of various string methods in Python, including capitalize(), count(), islower(), isupper(), swapcase(), istitle(), startswith(), endswith(), isnumeric(), isalpha(), isalnum(), find(), index(), replace(), and strip(). Each method is illustrated with code snippets and their corresponding outputs. The document serves as a practical guide for understanding and using string manipulation functions in Python.
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)
4 views4 pages

Class8&9 String Methods

The document provides examples of various string methods in Python, including capitalize(), count(), islower(), isupper(), swapcase(), istitle(), startswith(), endswith(), isnumeric(), isalpha(), isalnum(), find(), index(), replace(), and strip(). Each method is illustrated with code snippets and their corresponding outputs. The document serves as a practical guide for understanding and using string manipulation functions in Python.
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
You are on page 1/ 4

In [1]: # captialize() - to make 1st letter from lowercase into uppercase

a="hey buddies"
print(a.capitalize())

Hey buddies

In [2]: # captialize() - make no change in case of integer


age="40 is my age"
print(age.capitalize())

40 is my age

In [10]: #count() - it will count how many times the specified words/letters accurs in the v
b= "welcome to Gurukula"
print(b.count("welcome"))
print(b.count("e"))
print(b.count("u"))
print(b.count("u",0,15))
print(b.count("u",0,17))

1
2
3
2
3

In [12]: #islower() - checks the strings are in lowercase and print true or false and lowe
c="pYthon for Toddlers"
d="pytHon for TODDdlers"
e="python for toddlers"
print(c.islower())
print(d.islower())
print(e.islower())

False
False
True

In [13]: #islower() - checks the all strings are in lowercase and print true or false and
c="pYthon for Toddlers"
d="pytHon for TODDdlers"
e="python for toddlers"
print(c.lower())
print(d.lower())
print(e.lower())

python for toddlers


python for todddlers
python for toddlers

In [14]: #isupper() - checks the all strings are in uppercase and print true or false and
c="pYthon for Toddlers"
d="pytHon for TODDdlers"
e="PYTHON FOR TODDLERS"
print(c.isupper())
print(d.isupper())
print(e.isupper())

False
False
True

In [15]: #isupper() - checks the all strings are in uppercase and print true or false and
c="pYthon for Toddlers"
d="pytHon for TODDdlers"
e="PYTHON FOR TODDLERS"
print(c.upper())
print(d.upper())
print(e.upper())

PYTHON FOR TODDLERS


PYTHON FOR TODDDLERS
PYTHON FOR TODDLERS

In [17]: #SWAPCASE() -it converts upper into lowercase and viceversa


f="PYTHON for TOOddlers"
print(f.swapcase())

python FOR tooDDLERS

In [19]: #istittle()- checks whether first letter of every words are captitalized or not and
g="welcome to gurukula"
print(g.istitle())
print(g.title())

False
Welcome To Gurukula

In [27]: #startswith()- cheks whether the mentioned string is the starting strings or not
var="hey how are you??"
print(var.startswith("hey"))
print(var.startswith("how"))
print(var.startswith("h"))
print(var.startswith("he"))

True
False
True
True

In [29]: #endswith()- cheks whether the mentioned string is the ending strings or not
var="hey how are you??"
print(var.endswith("you"))
print(var.endswith("??"))
print(var.endswith("?"))
print(var.endswith("."))

False
True
True
False
In [4]: #class9 starts from here
#isnumeric() - check all given values are numeric aor not
a="123"
print(a.isnumeric())
a="abc"
print(a.isnumeric())
a="123abc"
print(a.isnumeric())
a=123
print(a.isnumeric())

True
False
False
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 9
7 print(a.isnumeric())
8 a=123
----> 9 print(a.isnumeric())

AttributeError: 'int' object has no attribute 'isnumeric'

In [6]: #isalpha() -check all given value are alphabets or not


a="abc"
print(a.isalpha())
a="123abc"
print(a.isalpha())

True
False

In [7]: #isalnum() - It checks 3 things 1. alphabets, 2.numeric , 3. alphanumeric . All the


abc="123gurukulam"
print(abc.isalnum())
alphabet="gurukulam"
print(alphabet.isalnum())
num="123"
print(num.isalnum())

True
True
True

In [14]: #find() - it finds where the given strings are suitated and fetch the index of the
txt="welcomw to gurukula"
print(txt.find("to"))
print(txt.find("u"))
print(txt.find("too")) # special case in find and ithis is not applicable for index

8
12
-1

In [13]: #index() - similar to find()


txt="welcomw to gurukula"
print(txt.index("to"))
print(txt.index("u"))
print(txt.index("too"))

8
12
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[13], line 5
3 print(txt.index("to"))
4 print(txt.index("u"))
----> 5 print(txt.index("too"))

ValueError: substring not found

In [17]: #replace() - it replaces the given strings with other strings


a="i love dogs"
print(a.replace("love", "like"))

i like dogs

In [26]: #replace() - it replaces the given strings with other strings


a="i love dogs,i love flowers, i love parrots"
print(a.replace("i", "we",2)) # it replace i in first two places only
print(a.replace("i", "we"))
print(a.replace("i", "we",1))
print(a.replace("i", "we",3))
print(a.replace("i", "we",0))

we love dogs,we love flowers, i love parrots


we love dogs,we love flowers, we love parrots
we love dogs,i love flowers, i love parrots
we love dogs,we love flowers, we love parrots
i love dogs,i love flowers, i love parrots

In [39]: #strip() ,lstrip(), rstrip()- whatever the special character mentioned inside the s
word="@#$python$$$@"
print(word.strip("@$#"))
print(word.lstrip("@$#")) # remove all mentioned special chartacters from left side
print(word.rstrip("@$#")) # remove all mentioned special chartacters from right sid

python
python$$$@
@#$python

In [44]: #ljust(), rjust() and center() - it add the special character


word= "python"
print(word.ljust(10,'$')) # 10 indicates the total numer of string length, python h
print(word.rjust(10,'$'))
print(word.center(10,'$'))
print(word.center(11,'$'))

python$$$$
$$$$python
$$python$$
$$$python$$

You might also like