Strings Assignment
Strings Assignment
In [ ]: str=input("enter a string:")
uc=0
lc=0
digits=0
for i in str:
if i.isupper():
uc+=1
elif i.islower():
lc+=1
elif i.isdigit():
digits+=1
words=str.split()
wordcount=len(words)
print("number of uppercase characters:",uc)
print("number of lowercase characters:",lc)
print("number of digits:",digits)
print("number of words are:",wordcount)
enter a string:Daksh909
number of uppercase characters: 1
number of lowercase characters: 4
number of digits: 3
number of words are: 1
In [ ]: str=input("enter a string:")
print("The total number of characters in the string are:",len(str))
enter a string:Daksh909
The total number of characters in the string are: 8
In [ ]: str=input("enter a string:")
if str.isdigit():
print("It contains only numbers")
else:
print("It does not contain only numbers")
enter a string:Daksh909
It does not contain only numbers
4.Write a program on how you would check if each word in a string begins with a
capital letter.
In [ ]: str=input("enter a string:")
if str.istitle():
print("Each word in the string begins with a capital letter")
else:
print("Each word in the string does not begin with a capital letter")
5.Write a program to find the index of the first occurence of a substring in a string.
In [ ]: str=input("enter a string:")
substring=input("enter the substring to be searched:")
index=str.find(substring)
print("The first occurence of the substring is at index:",index)
In [ ]: str=input("enter a string:")
a=str.capitalize()
print(a)
enter a string:daksh909
Daksh909
7.Write a program that takes a string with multiple words and then capitalizes the first
letter of each word and forms a new string out of it.
In [ ]: str=input("enter a string:")
print(str.title())
8.Write a program to enter string and find the number of occurences of any word.
In [ ]: str=input("Enter a string:")
word=input("enter the word to be checked:")
a = str.split()
count = 0
for i in a:
if i==word:
count+=1
print("The number of occurences of the word",word,"is",count)
In [ ]: #(a)
"Hello World".upper().lower()
'hello world'
Out[ ]:
In [ ]: #(b)
"Hello World".lower().upper()
'HELLO WORLD'
Out[ ]:
In [ ]: #(c)
"Hello World".find("Wor",1,6)
-1
Out[ ]:
In [ ]: #(d)
"Hello World".find("Wor")
6
Out[ ]:
In [ ]: #(e)
"Hello World".find("wor")
-1
Out[ ]:
In [ ]: #(f)
"Hello World".isalpha()
False
Out[ ]:
In [ ]: #(g)
"Hello World".isalnum()
False
Out[ ]:
In [ ]: #(h)
"1234".isdigit()
True
Out[ ]:
In [ ]: #(i)
"123GH".isdigit()
False
Out[ ]:
10. Write a program that takes a sentence as an input parameter where each word in
the sentence is separated by a space. The program should replace each space with
hyphen and return the modify sentence.