[go: up one dir, main page]

0% found this document useful (0 votes)
13 views7 pages

Python String Manipulation - W1

Chapter 6 focuses on string manipulation in programming, providing multiple-choice questions and practical exercises related to string slicing and functions. It covers topics such as extracting substrings, reversing strings, checking for palindromes, and using various string methods like count, replace, and strip. The chapter also includes tasks for users to implement string operations and analyze string properties.

Uploaded by

r5fkgm9jjd
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)
13 views7 pages

Python String Manipulation - W1

Chapter 6 focuses on string manipulation in programming, providing multiple-choice questions and practical exercises related to string slicing and functions. It covers topics such as extracting substrings, reversing strings, checking for palindromes, and using various string methods like count, replace, and strip. The chapter also includes tasks for users to implement string operations and analyze string properties.

Uploaded by

r5fkgm9jjd
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/ 7

CHAPTER 6: STRING MANIPULATION

II. Choose the correct option:


Q1. What is the output of the following code?

s = "Hello, World!"
print(s[7:])

A) Hello
B) World!
C) , World!
D) Hello,

Q2. What is the output of the following code?

s = "Python Programming"
print(s[:6] + " is fun")

A) Python is fun
B) Python Programming is fun
C) Programming is fun
D) thon is fun

Q3. Which of the following methods is used to remove whitespace from the beginning
and end of a string?
A) strip()
B) lstrip()
C) rstrip()
D) trim()

Q4. What is the output of the following code?

s = "banana"
print(s.count("a"))

A) 1
B) 2
C) 3
D) 4

Q5. How can you convert the string s = "python" to uppercase?

A) s.toUpper()
B) s.upper()
C) s.uppercase()
D) uppercase(s)

Q6. What is the output of the following code?

s = "abcd"
print(s * 3)

A) abcdabcdabcd
B) abcd * 3
C) abcd abcd abcd
D) Error

Q7. Which of the following will correctly check if a string ends with a specific
substring?

A) s.ends_with("txt")
B) s.endswith("txt")
C) endswith(s, "txt")
D) s.endswiths("txt")

Q8. What does the following code do?

s = "hello world"
print(s.replace("world", "Python"))
A) Replaces all occurrences of "hello" with "Python"
B) Replaces "world" with "Python"
C) Replaces "Python" with "world"
D) Throws an error

Q9. Which of the following will convert the first character of each word in the string s =
"hello world" to uppercase?

A) s.toTitle()
B) s.capitalize()
C) s.title()
D) title(s)

Q10. What is the output of the following code?

s = "abracadabra"
print(s.find("ra"))

A) 2
B) 4
C) 7
D) 5

Q11. What is the output of the following code?

s = "Hello, World!"
print(s[7:])

A) Hello
B) World!
C) , World!
D) Hello,

Q12. What is the output of the following code?

s = "Python Programming"
print(s[:6] + " is fun")

A) Python is fun
B) Python Programming is fun
C) Programming is fun
D) thon is fun

Q13. Which of the following methods is used to remove whitespace from the beginning
and end of a string?
A) strip()
B) lstrip()
C) rstrip()
D) trim()

Q14. What is the output of the following code?

s = "banana"
print(s.count("a"))

A) 1
B) 2
C) 3
D) 4

Q15. How can you convert the string s = "python" to uppercase?

A) s.toUpper()
B) s.upper()
C) s.uppercase()
D) uppercase(s)

Q16. What is the output of the following code?

s = "abcd"
print(s * 3)

A) abcdabcdabcd
B) abcd * 3
C) abcd abcd abcd
D) Error

Q17. Which of the following will correctly check if a string ends with a specific
substring?

A) s.ends_with("txt")
B) s.endswith("txt")
C) endswith(s, "txt")
D) s.endswiths("txt")

Q18. What does the following code do?

python
Copy code
s = "hello world"
print(s.replace("world", "Python"))

A) Replaces all occurrences of "hello" with "Python"


B) Replaces "world" with "Python"
C) Replaces "Python" with "world"
D) Throws an error

Q19. Which of the following will convert the first character of each word in the string s
= "hello world" to uppercase?

A) s.toTitle()
B) s.capitalize()
C) s.title()
D) title(s)

Q20. What is the output of the following code?

s = "abracadabra"
print(s.find("ra"))

A) 2
B) 4
C) 7
D) 5

III. Solve the following String Slicing and String Function Questions:

1. Given the string s = "PythonProgramming", write code to:

 Extract the substring "Python".


 Extract "Programming".
 Reverse the string.
 Extract every second character from "PythonProgramming".
 Extract the substring "gram" from "Programming" using slicing.

2. Write a program to check if a string is a palindrome using slicing. A palindrome reads


the same backward as forward (e.g., "madam").

3. For the string s = "LearningPythonIsFun":

 Print the first 8 characters.


 Print the last 5 characters.
 Print the string in reverse order.
 Extract characters from index 2 to 12 with a step of 2.

4. Given s = "abcdefghij", use slicing to:

 Extract the first 5 characters.


 Extract the last 3 characters.
 Extract characters from index 3 to 7 (inclusive).
 Extract every third character starting from the first.

5. Write a program to accept a string input from the user and print:

 The middle character(s) (for odd and even lengths).


 The string excluding the first and last characters.

String Function Questions:

6. Given the string s = "Hello, World!", write code to:

 Convert the string to uppercase.


 Convert the string to lowercase.
 Replace "World" with "Python".
 Count the occurrences of the letter "o".
 Check if the string starts with "Hello" and ends with "!".

7. Write a program to input a string and:

 Remove any leading or trailing spaces using strip().


 Replace all spaces with hyphens (-).
 Count the number of words in the string (hint: use split()).

8. Given a string s = "the quick brown fox", write code to:

 Capitalize the first letter of each word.


 Find the position of the word "brown".
 Check if the word "dog" exists in the string.

9. For the string s = "Python Programming is awesome":

 Split the string into a list of words.


 Join the list of words back into a single string using " ".
 Replace "awesome" with "fun".

10. Write a program to count the number of vowels in a given string. The program should:

 Accept a string from the user.


 Use a loop and the count() function to count vowels (a, e, i, o, u).

11. Write a program to input a sentence and:

 Reverse each word individually (e.g., "Hello World" → "olleH dlroW").


 Reverse the order of words in the sentence (e.g., "Hello World" → "World
Hello").

12. Create a program to check if a string contains only alphabets (isalpha()), only digits
(isdigit()), or alphanumeric characters (isalnum()).

13. Write a program to count the number of uppercase and lowercase letters in a string.

14. Given s = "Hello123", use string functions to:

 Extract all alphabetic characters.


 Extract all numeric characters.
 Check if the string is entirely alphanumeric.

15. Write a function remove_duplicates(s) that removes duplicate characters from a string
and returns the result. For example:

 Input: "banana"
 Output: "ban"

**************************************************************

You might also like