Showing posts with label Python String. Show all posts
Showing posts with label Python String. Show all posts

Wednesday, 22 November 2023

Python String MCQs Set 1

MCQs based on Python String


1. Which of the following methods is used to convert a string to uppercase?

A.   upper()

B.    capitalize()

C.    title()

D.   all()

 

2. Which of the following methods is used to remove whitespace from the beginning and end of a string?

A.   lstrip()

B.    rstrip()

C.    strip()

D.   replace()

 

3. Which of the following methods is used to count the number of occurrences of a substring in a string?

A.   count()

B.    find()

C.    index()

D.   rfind()

 

4. Which of the following methods is used to split a string into a list of words?

A.   split()

B.    splitlines()

C.    partition()

D.   rsplit()

 

5. Which of the following methods is used to format a string with placeholder values?

A.   format()

B.    f-strings

C.    % operator

D.   str.format()

 

6. Which of the following is the correct syntax for replacing all occurrences of a substring within a string with another substring?

A.   string.replace(old, new)

B.    replace(string, old, new)

C.    string.replaceAll(old, new)

D.   string.replaceSubstring(old, new)


7. Which of the following is the correct syntax for splitting a string into a list of words based on spaces?

A.   string.split()

B.    split(string)

C.    string.splitWords()

D.   string.wordSplit()

 


Answers:

1. (A) upper()

2. (C) strip()

3. (A) count()

4. (A) split()

5. (D) str.format()

6. (A) string.replace(old, new)

7. (A) string.split()


< Back to Python MCQs >


 

Monday, 11 July 2022

Python program to reverse a given string

  Python String  

Python program to reverse a given string. 

This program will read a string from user and prints reversed string.

Program Code :

# Python program to reverse a given string.

str=input("Enter your string:")
print("The original string is : " + str)

str_rev = str[::-1]
print("The reversed string is : " + str_rev)

Program Output :

Enter your string:Python Practicals 

The original string is : Python Practicals 

The reversed string is : slacitcarP nohtyP

Recommended Articles:

 


 Home 

 Next