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

Module 3.1 String Programs

vtu intro to python

Uploaded by

halomoon288
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)
32 views3 pages

Module 3.1 String Programs

vtu intro to python

Uploaded by

halomoon288
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

I. Read a string „Hello World!

‟ through the user console and perform the following


operations

val=input("Enter the string")


1. Access the character „W‟ using positive indexing
print(val[6])
2. Access the character „e‟ using negative indexing
print(val[-11])
3. Create any substring of length 5 using slicing
print(val[0:5])
4. Find the length of the string
print(len(val))
5. Use ‟ in‟ operator to check whether the substring „cat‟ is present in the given string
print('cat' in val)
6. Use ‟not in‟ operator to check whether the substring „orld‟ is present in the given string
print('orld' not in val)
7. Convert all the characters in the given string into uppercase and store it in a variable
spam1.
spam1=val.upper()
print(spam1)
8. Convert all the characters in the given string into lowercase and store it in a variable
spam2.
spam2=val.lower()
print(spam2)
9. Check if the given string contains all uppercase letters.
if val.isupper():
print(“All the characters in the given string are in uppercase”)
else:
print(“All the characters in the given string are not in uppercase”)
10. Check if the string spam2 contains all lowercase letters.

if val.islower():
print(“All the characters in the given string are in lowercase”)
else:
print(“All the characters in the given string are not in lowercase”)
11. Check if the given string starts with „Hello‟
if val.startswith(„Hello‟):
print(„The given string starts with Hello‟)
else:
print(„The given string not starts with Hello‟)
12. Check if the given string ends with „World!
if val.endswith(„World!‟):
print(„The given string ends with World!‟)
else:
print(„The given string not ends with World!‟)

II. Read a string and check whether the string is alphabet, alphanumerical, decimal, space
or title case using if statement.

i/p: „Hello‟, „hello‟, „Hello123‟,‟12345‟, „ ‟

val=input('Enter the string')

if val.isalpha():

print("The given string is alphabet")

if val.isalnum():

print("The given string is alphanumeric")

if val.isdecimal():
print("The given string is decimal")

if val.isspace():

print("The given string is space")

if val.istitle():

print("The given string is in title case")

III. Join the below given list of strings into single string value. Each string in the list is
separated by ***
list=[“cat”,”bat”,”rat”,”elephant”]
val=“***”.join(list)
print(val)

IV. Consider the given string and perform the following operations
Spam=‟ HelloABC, How are ABCyou „
a) Split the string using whitespace
b) Split the string using the delimiter ABC
c) Remove the white space from the left side of the string
d) Remove the white space from the right side of the string
e) Remove the white space from both left and right side of the string

Spam=' HelloABC, How are ABCyou '


print(Spam.split())
print(Spam.split('ABC'))
print(Spam.rstrip())
print(Spam.lstrip())
print(Spam.strip())

You might also like