DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 21- String Functions

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")
Enter fullscreen mode Exit fullscreen mode
Title is there

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
I like apples

Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode
135098294846640
135098294846640
135098294846640
135098294846640
135098292962352
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
12
12
Enter fullscreen mode Exit fullscreen mode
txt = "Mi casa, su casa."

x = txt.rfind("basa")
print(x) 
x = txt.rindex("basa")
print(x)
Enter fullscreen mode Exit fullscreen mode
-1
ValueError: substring not found
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
-1 or ValueError
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Today
is
Wednesday

Enter fullscreen mode Exit fullscreen mode

Top comments (0)