w3resource

PCEP Practice Test: Basic String Functions and Methods in Python

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "basic string functions and methods." The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.

Question 1: What is the result of the following code?

text = "hello"
print(text.upper())
  1. HELLO
  2. hello
  3. Hello
  4. Error

Answer: A) HELLO

Explanation: The upper() method converts all characters in the string to uppercase.

Question 2: Which of the following methods return a string in all lowercase letters? (Choose all that apply)

  • text.lower()
  • text.casefold()
  • text.capitalize()
  • text.swapcase()

Answer: A) text.lower()
B) text.casefold()

Explanation: Both lower() and casefold() convert a string to lowercase. capitalize() only capitalizes the first letter, and swapcase() swaps the case of each character.

Question 3: Complete the code to find the index of the first occurrence of the substring "world" in the string text.

text = "Hello, world!"
index = text.______("world")
print(index)

Answer: find

Explanation: The find() method returns the index of the first occurrence of the specified substring. If the substring is not found, it returns -1.

Question 4: What will be the output of the following code?

text = "Hello, World!"
print(text.replace("World", "Python"))
  1. Hello, Python!
  2. Hello, world!
  3. Hello,
  4. Python, World!

Answer: A) Hello, Python!

Explanation: The replace() method replaces the substring "World" with "Python", resulting in the output "Hello, Python!".

Question 5: Insert the correct method to remove leading and trailing whitespace from the string text.

text = "  Hello, Python!  "
clean_text = text.______()
print(clean_text)

Answer: strip

Explanation: The strip() method removes both leading and trailing whitespace from the string.

Question 6: Which of the following methods checks if a string ends with a specified substring?

  1. endswith()
  2. startswith()
  3. find()
  4. index()

Answer: A) endswith()

Explanation: The endswith() method checks if a string ends with the specified suffix.

Question 7: Which of the following methods can be used to remove specific characters from a string? (Choose all that apply)

  1. strip()
  2. replace()
  3. lstrip()
  4. rstrip()

Answer: A) strip()
B) replace()
C) lstrip()
D) rstrip()

Explanation: The strip(), lstrip(), and rstrip() methods can remove whitespace or specified characters. The replace() method can remove characters by replacing them with an empty string.

Question 8: Arrange the steps to correctly replace a substring in a string.

  1. Pass the old substring and the new substring as arguments.
  2. Call the replace() method.
  3. Assign the result to a new variable.

Answer: 2, 1, 3,

Explanation: First, call the replace() method, then pass the old and new substrings, and finally assign the result to a new variable.

Question 9: Complete the code to count the occurrences of the letter "o" in the string text.

text = "Hello, World!"
count = text.______("o")
print(count)

Answer: count

Explanation: The count() method returns the number of occurrences of a specified substring in a string.

Question 10: What will be the output of the following code?

text = "hello"
print(text.capitalize())
  1. Hello
  2. HELLO
  3. hello
  4. Error

Answer: A) Hello

Explanation: The capitalize() method converts the first character to uppercase and the rest to lowercase.

Question 11: Insert the correct method to check if the string text starts with "Hello".

text = "Hello, World!"
result = text.______("Hello")
print(result)

Answer: startswith

Explanation: The startswith() method checks if a string starts with the specified prefix.

Question 12: What will be the result of the following code?

text = "Python programming"
print(text.split())
  1. ['Python', 'programming']
  2. B) ['P', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
  3. ['Python', 'programming', '']
  4. ['Python programming']

Answer: A) ['Python', 'programming']

Explanation: The split() method splits the string by whitespace into a list of words.

Question 13: Which of the following string methods modify the string by returning a new version of it? (Choose all that apply)

  1. upper()
  2. replace()
  3. split()
  4. index()

Answer: A) upper()
B) replace()
C) split()

Explanation: The upper(), replace(), and split() methods return a modified version of the string. The index() method returns the position of a substring, not a modified string.

Question 14: Arrange the steps to correctly split a sentence into words and store them in a list.

  1. Call the split() method.
  2. Print the list of words.
  3. Assign the result to a variable.

Answer: 1, 3, 2

Explanation: First, call the split() method, then assign the result to a variable, and finally print the list of words.

Question 15: Complete the code to check if the string text contains only digits.

text = "12345"
result = text.______()
print(result)

Answer: isdigit

Explanation: The isdigit() method checks if all characters in the string are digits.

Question 16: What will be the output of the following code?

text = " Hello, Python! "
print(text.strip())
  1. Hello, Python!
  2.     Hello, Python!
  3. Hello,Python!
  4. Error

Answer: A) Hello, Python!

Explanation: The strip() method removes leading and trailing whitespace from the string.

17. Insert the correct method to convert all characters in the string text to lowercase.

text = "PYTHON"
lower_text = text.______()
print(lower_text)

Answer: lower

Explanation: The lower() method converts all characters in a string to lowercase.

Question 18: Which of the following methods checks if a string contains only alphabetic characters?

  1. isalnum()
  2. isalpha()
  3. isdigit()
  4. isspace()

Answer: B) isalpha()

Explanation: The isalpha() method checks if all characters in the string are alphabetic.

Question 19: Which of the following methods can be used to check properties of a string? (Choose all that apply)

  • isalpha()
  • isalnum()
  • isdigit()
  • islower()

Answer: A) isalpha()
B) isalnum()
C) isdigit()
D) islower()

Explanation: All these methods are used to check specific properties of a string: isalpha() checks for alphabetic characters, isalnum() checks for alphanumeric characters, isdigit() checks for digits, and islower() checks for lowercase characters.

Question 20: Arrange the steps to check if a string contains only lowercase characters.

  1. Call the islower() method.
  2. Assign the string to a variable.
  3. Print the result of the islower() method.

Answer: 2, 1, 3

Explanation: First, assign the string to a variable, then call the islower() method, and finally print the result.

Question 21: Complete the code to check if the string text contains only alphabetic characters and digits.

text = "Python3"
result = text.______()
print(result)

Answer: isalnum

Explanation: The isalnum() method checks if the string contains only alphanumeric characters (letters and digits).

Question 22: What will be the output of the following code?

text = "HELLO"
print(text.isupper())
  1. True
  2. False
  3. HELLO
  4. Error

Answer: A) True

Explanation: The isupper() method checks if all characters in the string are uppercase, and since they are, the result is True.

Question 23: Insert the correct method to convert the first character of the string text to uppercase and the rest to lowercase.

text = "python"
result = text.______()
print(result)

Answer: capitalize

Explanation: The capitalize() method converts the first character of the string to uppercase and the remaining characters to lowercase.

Question 24: What will be the result of the following code?

text = "Python Programming"
print(text.find("Pro"))
  1. -1
  2. 7
  3. 5
  4. Error

Answer: B) 7

Explanation: The find() method returns the index of the first occurrence of the substring "Pro" in the string, which starts at index 7.ne breaks.

Question 25: Which of the following methods return a boolean value? (Choose all that apply)

  1. startswith()
  2. endswith()
  3. islower()
  4. find()

Answer: A) startswith()
B) endswith()
C) islower()

Explanation: The startswith(), endswith(), and islower() methods return boolean values (True or False). The find() method returns the index of a substring.



Become a Patron!

Follow us on Facebook and Twitter for latest update.