[Go to site: main page, start]

0% found this document useful (0 votes)
3 views5 pages

Essential Python String Functions Guide

Uploaded by

yashagrawal34882
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Essential Python String Functions Guide

Uploaded by

yashagrawal34882
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 len()

2 lower()
3 upper()
4 stratswith()
5 endswith()
6 isalpha()
7 islower()
8 isupper()
9 isdigit()
10 count() This function is used to count the number of times a
character is present in a word or number of times a word is
present in sentence.
Ex-print("SAMBALPUR".count("A")) Result-2
Ex-print("ONE GOOD BOY AND ONE BEST
BOY".count("BOY")) Result-2
11 join() This function is used to add a character or a set of
character in between every two character of the given
string. Ex-print("*".join("TIGER")) Result- T*I*G*E*R
12 swapcase() Converts capital letters into small and small to capital
Ex-Print(“SAMbalPUr”.swapcase()) Result- samBALpuR
13 replace() This function is used to replace a character by another
character in a string/word or a word by another word in a
sentence.
Ex- print("SAMBALPUR".replace("A","E")) SEMBELPUR
print("ONE FAT BOY AND ONE THIN BOY
HERE".replace("BOY","GIRL"))
Result- ONE FAT GIRL AND ONE THIN GIRL HERE
14 find() This function is used to find a character in a word or word
in sentence, if it is present then it gives it starting index
position, if it is not present than it gives -1.
Ex- print("SAMBALPUR".find("A")) Result 1
print("ONE BOY AND ANOTHER BOY HERE".find("BOY"))
Result-4
print("ONE BOY AND ANOTHER BOY HERE".find("TOY")
Result= -1
15 title() This function is used to convert starting letter of every word
into capital in a sentence.
Ex- print("ONE BOY AND ANOTHER BOY HERE".title())
Result- One Boy And Another Boy Here
Ex-print("some good boys".title()) Some Good Boys
16 capitalize() This function is used to convert first letter of the sentence
in capital rest changes to small.
Ex-print("some good boys".title()) Result-Some good boys
Ex- print("SOME GOOD BOYS".capitalize())
Result-Some Good Boys
17 strip() This function is used to remove the blank spaces present
at the beginning and ending of the string (prefix / suffix)
Ex- n=”- -SAMBALPUR CITY- - - “
x=len(n) Length-19 (Including blank spaces)
m=[Link]() now m= “SAMBALPUR CITY”
y=len(m) y=14 (excluding blank spaces)
18 reversed()
19 print("god" * 3) Result- godgodgod
chr()
20 ord() print("".join(reversed("TIGER")))-Result-REGIT
21
22

String Functions

PROGRAMS OF STRINGS
1) Convert the following based on users choice
Choice-1 to enter a word in capital letter and convert into small letter
Choice-2 to enter a word in small letter and convert into capital letter
2) Enter any word the find the following based on user’s choice
Choice-to print its 1st three characters
Choice-2 to print its last three characters
For any other choice print “Word is invalid”
3) Print the following based on user’s choice
Choice-1 to print capital letters and their ASCII code
Choice-2 to print small letters and their ASCII code
Choice-3 print digits (from 0-9) and their ASCII code
For any other choice print “Choice is invalid”
4) Enter any word and count the number of vowels and number of consonants present
in the word.
5) Enter any word with capital and small letters and count how many capital letters and
how many small letters are present in the word.
6) Enter any string with mixed characters (with capital letters, small letters, digits and
special letters , count them separately.
7) Enter any word with capital letters and small letters, convert capital letters into small
and small letters into capital. Input-“SAMbal” Output-“samBAL”
8) Enter any word and check whether it is special word or not. When the 1st and last
letter of the word is same then it is known as special word. Example-“RAIPUR”
9) Enter any word any check whether it is “Palindrome Word” or not , when the original
word and it’s reverse of the is same it is known as “Palindrome” Ex-“MADAM”
10) Enter any word and check the is valid word or invalid word , when the word contains
at least one vowel the it called valid Example-“TIGER” Valid “RHYTHM” – invalid
11) Enter any word and encode the word as follows (replace each character with next
third character) “Input-“DAYMOZE” Output- FCAOQBG
12) Enter any word and Decode the word as follows (replace each character with
previous third character) “Input-“DAYBEM” Output-BYWZDK
13) Enter any word and find its length the length is even then break the word in two
halves and print two halves. if the length is odd then print “word is invalid”
14) Enter any sentence and a word if it is present then print “Search is successful” else
print search is un successful
15) Enter any word and print the alphabets of the word in ascending order
Input – ROZATE Output- AEORTZ
16) Enter any word and print the alphabets of the word in descending order
Input – ROZATE Output- ZTROEA
17) Enter any word and remove all those character which are repeated in the word.
Input- “ANAMOURMEM” Output- NOURE
18) Enter any word/sentence and print the frequency of each alphabet present in the
word or sentence.
19) Enter and sentence and a word , if the word is present then replace it with the old
word Input-“ONE GOOD BOY BUT BOY PLAY” Word=”BOY” Replacing word-GIRL
Output-“ ONE GOOD GIRL BUT GIRL PLAY”
If the word is not present then print the original sentence.
20) Enter any word and replace the vowel with it’s next character.
Input-“COMPUTER” Output-“CPMPVTFR”

PROGRAMS-11
m=""
n=str(input("Enter any word"))
n=[Link]()
l=len(n)
for a in range(0,l):
x=n[a]
y=ord(x)
if y>=65 and y<=87:
y=y+2
elif y==89:
y=65
else:
y=66
z=chr(y)
m=m+z
print("Word in encoded form ",m)
Input- RAYMAZE Output- TCAOCBG
PROGRAMS-9
n=str(input("enter any word"))
n=[Link]()
m="".join(reversed(n))
if m==n:
print("word is pallindrome")
else:
print("word is not pallindrome")
PROGRAMS-15
n=str(input("Enter any word"))
n=[Link]()
l=len(n)
for a in range(65,91):
for b in range(0,l):
x=n[b]
y=ord(x)
if y==a:
print(x)

PROGRAMS-16
n=str(input("Enter any word"))
n=[Link]()
l=len(n)
for a in range(90,64,-1):
for b in range(0,l):
x=n[b]
y=ord(x)
if y==a:
print(x)

You might also like