Home > Backend Development > Python Tutorial > Day String Functions

Day String Functions

Susan Sarandon
Release: 2024-12-21 02:12:09
Original
438 people have browsed it

Day  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")
Copy after login
Title is there

Copy after login

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)
Copy after login
I like apples

Copy after login

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))
Copy after login
135098294846640
135098294846640
135098294846640
135098294846640
135098292962352
Copy after login

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)
Copy after login
12
12
Copy after login
txt = "Mi casa, su casa."

x = txt.rfind("basa")
print(x) 
x = txt.rindex("basa")
print(x)
Copy after login
-1
ValueError: substring not found
Copy after login

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')
Copy after login
-1 or ValueError
Copy after login

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
Copy after login
Today
is
Wednesday

Copy after login

The above is the detailed content of Day String Functions. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template