0% found this document useful (0 votes)
157 views3 pages

Strings Assignment

The document contains programs to perform various string operations in Python. It includes programs to count characters in a string, check if a string contains only numbers, capitalize the first letter of words in a string, find the index of a substring in a string, and replace spaces with hyphens in a sentence. The document provides sample inputs and outputs to demonstrate the behavior of each program.

Uploaded by

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

Strings Assignment

The document contains programs to perform various string operations in Python. It includes programs to count characters in a string, check if a string contains only numbers, capitalize the first letter of words in a string, find the index of a substring in a string, and replace spaces with hyphens in a sentence. The document provides sample inputs and outputs to demonstrate the behavior of each program.

Uploaded by

DAKSH SACHDEV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Strings Assignment

-By Daksh Sachdev XI - A

1.Write a program to enter a string and count how many


uppercase,lowercase,digits,words are present in it.

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

2. Write a program to count the total number of characters in a string.

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

3. Write a program to check if a string contains only numbers.

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")

enter a string:Phy,Chem.Math,Comp Science And Eng Are Easy


Each word in the string begins 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)

enter a string:Hello everybody


enter the substring to be searched:body
The first occurence of the substring is at index: 11

6.Write a program to capitalize the first character of a string.

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())

enter a string:chess is the best game in the world


Chess Is The Best Game In The World

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)

Enter a string:hello world, hello python, hello everyone


enter the word to be checked:hello
The number of occurences of the word hello is 3

9.What would the following expressions return?

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.

In [ ]: str = input("enter a string:")


print(str.replace(" ","-"))

enter a string:chess is the best


chess-is-the-best

In [ ]: !jupyter nbconvert --to html /content/Strings.ipynb

[NbConvertApp] Converting notebook /content/Strings.ipynb to html


[NbConvertApp] Writing 606711 bytes to /content/Strings.html

You might also like