DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

1

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

๐Ÿ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay