You are on page 1of 16

6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

1. Capitalize

Convert only first letter of string in upper case.

Example-1

In [7]:

info = 'my name is vrushali. my age is 28'


info

Out[7]:

'my name is vrushali. my age is 28'

In [8]:

info = info.capitalize() ##
print(info)

My name is vrushali. my age is 28

For number

In [14]:

age = '28 is my age'


age

Out[14]:

'28 is my age'

In [43]:

age = age.capitalize() ## Remains same for the string starting with number.
print(age)

28 is my age

Example-2

In [21]:

institute = 'welcome to the excelr'


institute

Out[21]:

'welcome to the excelr'

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 1/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [44]:

institute = institute.capitalize()
print(institute)

Welcome to the excelr

2. Casefold

Converts whole string into lower case letters

Example -1

In [27]:

AI = 'Artificial intelligence (AI) is intelligence demonstrated by machines.'


AI

Out[27]:

'Artificial intelligence (AI) is intelligence demonstrated by machines.'

In [45]:

AI = AI.casefold()
print(AI)

artificial intelligence (ai) is intelligence demonstrated by machines.

Example -2

In [32]:

goal = 'mY GoaL IS To beCoME BEst DaTA SCIeNtIst IN 2022'


goal

Out[32]:

'mY GoaL IS To beCoME BEst DaTA SCIeNtIst IN 2022'

In [46]:

goal = goal.casefold()
print(goal)

my goal is to become best data scientist in 2022

3. lower

converts whole string in lower case

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 2/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [79]:

sub = 'MY FAVOURITE SUBJECT IS MATH'


sub

Out[79]:

'MY FAVOURITE SUBJECT IS MATH'

In [80]:

sub = sub.lower()
print(sub)

my favourite subject is math

4. upper

Converts whole string into upper case

In [81]:

weather = "Today's weather is so cloudy"


weather

Out[81]:

"Today's weather is so cloudy"

In [82]:

weather = weather.upper()
print(weather)

TODAY'S WEATHER IS SO CLOUDY

5. title

Converts first letter of each word in upper case of the string.

In [86]:

hobby = 'my hobby is to visit new beautiful locations mostly in winter & rainy season'
hobby

Out[86]:

'my hobby is to visit new beautiful locations mostly in winter & rainy seaso
n'

In [87]:

hobby = hobby.title()
print(hobby)

My Hobby Is To Visit New Beautiful Locations Mostly In Winter & Rainy Season

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 3/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

6. swapcase

Converts lower case to upper & upper case to lower in the string

In [88]:

websites = 'There are so many websites to learn PYTHON'


websites

Out[88]:

'There are so many websites to learn PYTHON'

In [89]:

websites = websites.swapcase()
print(websites)

tHERE ARE SO MANY WEBSITES TO LEARN python

7. Center

It converts string in centered location.

Example-1

In [61]:

title = 'Chapter_No_1_Python'
title

Out[61]:

'Chapter_No_1_Python'

In [62]:

title_1 = title.center(29)
print(title_1)

Chapter_No_1_Python

In [64]:

title_2 = title.center(33,'*')
print(title_2)

*******Chapter_No_1_Python*******

Example-2

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 4/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [93]:

book = 'Wings_of_fire'
book

Out[93]:

'Wings_of_fire'

In [94]:

book = book.center(30,'-')
print(book)

--------Wings_of_fire---------

8. Count

show the no. of times the value occurs in string

Example-1

In [113]:

news_def='News is information about current events. News is sometimes called "hard news" to
news_def

Out[113]:

'News is information about current events. News is sometimes called "hard ne


ws" to differentiate it from soft media.'

In [114]:

news_def = news_def.count('News')
print(news_def)

Example - 2

In [118]:

num = '1,2,2,3,5,6,2,8,3,6,9,2,8,9'
num

Out[118]:

'1,2,2,3,5,6,2,8,3,6,9,2,8,9'

In [119]:

num = num.count('2')
print(num)

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 5/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

9. find

Searches the value & returns the position of the value i.e index number

Example - 1

In [31]:

life = 'Life is very Beautiful'


life

Out[31]:

'Life is very Beautiful'

In [32]:

life_1= life.find('very')
print(life_1)

In [33]:

life_2= life.find('q')
print(life_2)

-1

Example-2

In [142]:

plants = 'I love plants & there are so many plants in my home'
plants

Out[142]:

'I love plants & there are so many plants in my home'

In [143]:

plants = plants.find('plants')
print(plants)

10. index

If the value is not found, the find() method returns -1, but the index() method will
raise an exception(error)

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 6/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [34]:

life

Out[34]:

'Life is very Beautiful'

In [36]:

print(life.find('w'))
print(life.index('w'))

-1

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

Input In [36], in <cell line: 2>()

1 print(life.find('w'))

----> 2 print(life.index('w'))

ValueError: substring not found

11. format

Example - 1

In [1]:

Emp_data = 'Emp_name is {}, Emp_iD is {}, Salary is {}'.format('Vrushali' ,93,900000)


print(Emp_data)

Emp_name is Vrushali, Emp_iD is 93, Salary is 900000

In [11]:

Emp_data_1 = 'Emp_name is {name}, Emp_iD is {no}, Salary is {pay}'.format(name ='Vrushali'


print(Emp_data_1)

Emp_name is Vrushali, Emp_iD is 93, Salary is 900000

In [6]:

fname = input('Name:')
emp_id = input('ID:')
pay = input('salary:')

print('Emp_name is {}, Emp_Id is {} & salary is {}'.format(fname,emp_id,pay))

Name:Vrushali

ID:15

salary:900000

Emp_name is Vrushali, Emp_Id is 15 & salary is 900000

Example - 2

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 7/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [16]:

ssc_res = 'pass'
hsc_res = 'pass'
ssc_per = input('Percentage_s:')
hsc_per = input('Percentage_h:')
print('My result of 10th is {} & I got {} % also my result of 12th is {} & I got {} %'.form

Percentage_s:93

Percentage_h:85

My result of 10th is pass & I got 93 % also my result of 12th is pass & I go
t 85 %

12. isalpha

Returns True if all characters in the string are in the alphabet

Example - 1

In [37]:

comp = 'Computer@2015'
comp

Out[37]:

'Computer@2015'

In [38]:

print(comp.isalpha())

False

Example - 2

In [43]:

social_media = 'Facebook'
social_media

Out[43]:

'Facebook'

In [44]:

print(social_media.isalpha())

True

13. isalnum

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 8/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

Returns True if all characters in the string are alphanumeric

Example - 1

In [51]:

season = 'Rainy123'
season

Out[51]:

'Rainy123'

In [52]:

print(season.isalnum())

True

In [54]:

colour = 'pink'

In [56]:

print(colour.isalnum())

True

In [57]:

value = '1,2,3,4,5,6,7,8,9'
value

Out[57]:

'1,2,3,4,5,6,7,8,9'

In [58]:

print(value.isalnum())

False

#### Example - 2

In [50]:

comp

Out[50]:

'Computer@2015'

In [53]:

print(comp.isalnum())

False

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-upp… 9/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

14. isascii
In [59]:

value

Out[59]:

'1,2,3,4,5,6,7,8,9'

In [60]:

print(value.isascii())

True

In [61]:

colour

Out[61]:

'pink'

In [63]:

print(colour.isascii())

True

15. isdecimal

The isdecimal() method returns True if all the characters are decimals (0-9)

In [64]:

x = '15'
y= '12.5'

In [99]:

print(x.isdecimal())
print(y.isdecimal())

True

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

Input In [99], in <cell line: 2>()

1 print(x.isdecimal())

----> 2 print(y.isdecimal())

AttributeError: 'float' object has no attribute 'isdecimal'

16. isdigit

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 10/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

The isdigit() method returns True if all the characters are digits, otherwise
False

In [73]:

array = '1,2,3'
array_1 = '100'
array_2 = 'Vrushali'

In [74]:

print(array.isdigit())
print(array_1.isdigit())
print(array_2.isdigit())

False

True

False

17. isidentifier

A string is considered a valid identifier if it only contains alphanumeric letters (a-


z) and (0-9), or underscores (_).

In [81]:

game = 'Carrom1_Hocky2_cricket3'
line = 'straight_line'
student = 'Vinit, Kirti, Ramya'
year = '2021' ## identifier never start with number or any space

In [82]:

print(game.isidentifier())
print(line.isidentifier())
print(student.isidentifier())
print(year.isidentifier())

True

True

False

False

18. islower

The islower() method returns True if all the characters are in lower case,
otherwise False

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 11/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [101]:

print(line)
print(game)
print(student)

straight_line

Carrom1_Hocky2_cricket3

Vinit, Kirti, Ramya

In [85]:

print(line.islower())
print(game.islower())
print(student.islower())

True

False

False

19. isnumeric

Returns True if all characters in the string are numeric

In [97]:

number = '123597'
numbers_1 = '1,2,2,3,5,6,98,5'
numbers_2 = ' 2,2.5,-6,9,58,47,95'
animals = 'Cat, Dog, Cow'

In [98]:

print(number.isnumeric())
print(numbers_1.isnumeric())
print(numbers_2.isnumeric())
print(animals.isnumeric())

True

False

False

False

20. isprintable

The isprintable() method returns True if all the characters are printable,
otherwise False.

Example of none printable character can be carriage return(\r) and line


feed(\n).

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 12/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [102]:

my_str = 'I am starting to read a new book'


print(my_str.isprintable())

True

In [103]:

menu_bar = 'start, edit, file, \nview, \rinsert'


print(menu_bar.isprintable())

False

In [104]:

para = '''I am going to talk with John sir


about doubt tommorow
'''
print(para.isprintable())

False

21. isspace

The isspace() method returns True if all the characters in a string are
whitespaces, otherwise False

In [111]:

sub_1 = ' marathi '


sub_2 = 'Data_Science, AI, Python'
sub_3 = ' '

In [112]:

print(sub_1.isspace())
print(sub_2.isspace())
print(sub_3.isspace())

False

False

True

22. istitle

The istitle() method returns True if all words in a text start with a upper case
letter, AND the rest of the word are lower case letters, otherwise False.

In [115]:

Tv = 'I Frequently Watched Tv'


SIRI = 'Get everyday tasks done with just your voice.'

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 13/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [116]:

print(Tv.istitle())
print(SIRI.istitle())

True

False

23. isupper

The isupper() method returns True if all the characters are in upper case,
otherwise False.

In [123]:

movies = 'BLACH, DHOOM_3, RRR, KGF_2'


colab = 'Colab is the commonly used abbreviation of the New York City'

In [124]:

print(movies.isupper())
print(colab.isupper())

True

False

24. join

The join() method takes all items in an iterable and joins them into one
string.

In [127]:

flowers = ('Rose', 'Tulip', "Jasmine", "Hibiscus")


data_types = ('int','float','string')

In [128]:

print('#'.join(flowers))
print('-'.join(data_types))

Rose#Tulip#Jasmine#Hibiscus

int-float-string

25. ljust

The ljust() method will left align the string, using a specified character (space is
default) as the fill character.

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 14/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [130]:

lang = 'python'
watch = 'Rolex'

In [136]:

print(lang.ljust(10), 'is easy to understand ')


print(watch.ljust(10,'-'), 'is my favourite brand')

python is easy to understand

Rolex----- is my favourite brand

26.rjust

Returns a right justified version of the string

In [135]:

print(lang.rjust(10,'*'), 'is easy to understand ')


print(watch.rjust(10), 'is my favourite brand')

****python is easy to understand

Rolex is my favourite brand

27. lstrip

The lstrip() method removes any leading characters (space is the default leading
character to remove)

In [140]:

location = '.....,,,agfsadfu==Andman'
ball = ' Tenis '

In [143]:

print(location.lstrip(".,agfsdu="))
print(ball.lstrip())

Andman

Tenis

28. rstrip

The rstrip() method removes any trailing characters (characters at the end a
string), space is the default trailing character to remove.

In [149]:

game =' Cricket is my favourite game '


trainer = 'John is very good trainer----****#dcegbdfgbd**'

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 15/16


6/29/22, 11:42 PM Day_1_String_Functions - Jupyter Notebook

In [150]:

print(game.rstrip())
print(trainer.rstrip('-*#decgbf'))

Cricket is my favourite game

John is very good trainer

localhost:8889/notebooks/Python by John/Day_1_String_Functions.ipynb#The-istitle()-method-returns-True-if-all-words-in-a-text-start-with-a-up… 16/16

You might also like