0% found this document useful (0 votes)
6 views1 page

String 1

The document provides answers to various questions related to string methods in Python, including the differences between find() and index() methods, string slicing, and appropriate functions for specific tasks. It also includes outputs for specific code snippets and a long answer question about calculating word lengths in a string. Key functions mentioned include isalnum(), find(), capitalize(), upper(), isupper(), and lstrip().

Uploaded by

Preetham Raaj
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)
6 views1 page

String 1

The document provides answers to various questions related to string methods in Python, including the differences between find() and index() methods, string slicing, and appropriate functions for specific tasks. It also includes outputs for specific code snippets and a long answer question about calculating word lengths in a string. Key functions mentioned include isalnum(), find(), capitalize(), upper(), isupper(), and lstrip().

Uploaded by

Preetham Raaj
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

Q6. What is difference between find( ) and index( ) string methods.

Give an example to
illustrate difference.
Q7. What is the output of "Hello"[1:4]?
Q8. Suggest appropriate functions for the following tasks –
(a) To check whether the string contains digits.
(b) To find the occurrence of a string within another string.
(c) To convert the first letter of a string to uppercase.
(d) To convert all the letters of a string to uppercase.
(e) To check whether all the letters of the string are in capital letters.
(f) To remove all the white spaces from the beginning of a string.
Q9. Find and write the output of the following python code:
x = "abcdef"
i = "a"
while i in x:
print(i, end = " ")
Q10 . Write the output of following code:
first= “Programming Language Learning work”
print([Link](“Language”))
ANSWERS
Ans 1: A string is a sequence of characters enclosed within single, double, or triple quotes.
Example:
s1 = 'Hello'
s2 = "World"
s3 = '''Triple quotes are also valid'''
Ans 2: Strings are immutable, meaning they cannot be changed after creation.
Example: s = "Hello"
s[0] = "h" # This will cause an error
Ans 3: s = "hello"
print([Link]()) # Output: HELLO
Ans 4: isalpha() is for alphabet-only strings, and isdigit() is for digit-only string
Ans 5: s = "banana"
print([Link]("a")) # Output: 3
Ans 6 : The find() method returns the index of the first occurrence of the substring. If the
substring is not found, it returns -1.
The index() method also returns the index of the first occurrence of the substring.
However, if the substring is not found, it raises a ValueError.
Example:

Ans 7 : Output: 'ell' (substring from index 1 to 3)


Ans 8: (a) isalnum() (b) find() (c) capitalize() (d) upper() (e) isupper() (f) lstrip()
Ans 9 : Ans: aaaaaa ---- OR infinite loop
Ans 10: ('Programming ', 'Language', ' Learning work')
LONG ANSWER QUESTIONS
Q 1. Write a program to input a string and calculate the length of each word present in
string and print length along with the word.

65 | P a g e

You might also like