Write a program to check the given key is title or not.
istitle()- Check the first letter of each word is capitalized, and all other letters in the word are lowercase.
txt = 'Rose Is A Beautiful Flower'
if txt[0]>='a' and txt[0]<='z':
print("No Title is there")
else:
i = 1
while i<len(txt)-1:
if txt[i]==' ':
if txt[i+1]>='A' and txt[i+1]<='Z':
pass
else:
print("No Title is there")
break
i+=1
else:
print("Title is there")
Title is there
Write a program to replace a word by another word.
replace()-replace occurrences of a substring within a string with another substring.
txt = "I like bananas"
already = "bananas"
new = "apples"
l = len(already) # l = 7
start = 0
end = l
while end<=len(txt):
if txt[start:end] == 'bananas':
txt = txt[:start] + new
start+=1
end+=1
else:
print(txt)
I like apples
In Python everything is an object.
Every object can create a different memory space.
String is immutable(Not changeable).
Identical objects can refer the same memory.
country1 = 'India'
country2 = 'India'
country3 = 'India'
country4 = 'India'
print(id(country1))
print(id(country2))
print(id(country3))
print(id(country4))
country1 = "Singapore"
print(id(country1))
135098294846640
135098294846640
135098294846640
135098294846640
135098292962352
If we try to edit an existing string,it won't get changed. Instead, a new memory will be created for storing the new value.
Difference between rfind() and rindex():
Both methods search for the last occurrence of a specified substring, but they behave differently when the substring is absent.
txt = "Mi casa, su casa."
x = txt.rfind("casa")
print(x)
x = txt.rindex("casa")
print(x)
12
12
txt = "Mi casa, su casa."
x = txt.rfind("basa")
print(x)
x = txt.rindex("basa")
print(x)
-1
ValueError: substring not found
rfind()-If not found: Returns -1
rindex()-If not found: Raises a ValueError
Write a program to check a given key is available or not.
(rfind() or rindex())
txt = "Python is my favourite language"
key = 'myy'
l = len(key)
start = 0
end = l
while end<=len(txt):
if txt[start:end] == key:
print(start)
break
start += 1
end += 1
else:
print('-1 or ValueError')
-1 or ValueError
Write a program to split given text.
split()- to divide a string into a list of substrings based on a specified separator.
txt = "Today is Wednesday"
word = ''
start = 0
i = 0
while i<len(txt):
if txt[i]==' ':
print(txt[start:i])
start = i+1
elif i == len(txt)-1:
print(txt[start:i+1])
i+=1
Today
is
Wednesday
Top comments (0)