Day - 1 - String - Functions - Jupyter Notebook
Day - 1 - String - Functions - Jupyter Notebook
1. Capitalize
Example-1
In [7]:
Out[7]:
In [8]:
info = info.capitalize() ##
print(info)
For number
In [14]:
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]:
Out[21]:
In [44]:
institute = institute.capitalize()
print(institute)
2. Casefold
Example -1
In [27]:
Out[27]:
In [45]:
AI = AI.casefold()
print(AI)
Example -2
In [32]:
Out[32]:
In [46]:
goal = goal.casefold()
print(goal)
3. lower
In [79]:
Out[79]:
In [80]:
sub = sub.lower()
print(sub)
4. upper
In [81]:
Out[81]:
In [82]:
weather = weather.upper()
print(weather)
5. title
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
6. swapcase
Converts lower case to upper & upper case to lower in the string
In [88]:
Out[88]:
In [89]:
websites = websites.swapcase()
print(websites)
7. Center
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
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
Example-1
In [113]:
news_def='News is information about current events. News is sometimes called "hard news" to
news_def
Out[113]:
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)
9. find
Searches the value & returns the position of the value i.e index number
Example - 1
In [31]:
Out[31]:
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]:
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)
In [34]:
life
Out[34]:
In [36]:
print(life.find('w'))
print(life.index('w'))
-1
---------------------------------------------------------------------------
1 print(life.find('w'))
----> 2 print(life.index('w'))
11. format
Example - 1
In [1]:
In [11]:
In [6]:
fname = input('Name:')
emp_id = input('ID:')
pay = input('salary:')
Name:Vrushali
ID:15
salary:900000
Example - 2
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
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
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
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
---------------------------------------------------------------------------
1 print(x.isdecimal())
----> 2 print(y.isdecimal())
16. isdigit
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
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
In [101]:
print(line)
print(game)
print(student)
straight_line
Carrom1_Hocky2_cricket3
In [85]:
print(line.islower())
print(game.islower())
print(student.islower())
True
False
False
19. isnumeric
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.
In [102]:
True
In [103]:
False
In [104]:
False
21. isspace
The isspace() method returns True if all the characters in a string are
whitespaces, otherwise False
In [111]:
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]:
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]:
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]:
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.
In [130]:
lang = 'python'
watch = 'Rolex'
In [136]:
26.rjust
In [135]:
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]:
In [150]:
print(game.rstrip())
print(trainer.rstrip('-*#decgbf'))