String manipulation interview questions – Python Interview Questions on String Manipulation

String manipulation interview questions: We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels.

Python Interview Questions on String Manipulation

Question 1.
Is .len( ) a valid string method?
Answer:
No. To get the length of a string using the function len( ), as in len(string)

Question 2.
What does the .title( ) method do?
Answer:
It returns a title-cased string, words start with upper case and everything else is lower case.

Question 3:
Given a string with leading and trailing spaces, illustrate how to return a string without the leading and trailing spaces.
Answer:
string.strip(”)

Question 4:
What is the difference between the find( ) and index( ) string methods?
Answer:
If the substring is not found, find( ) will return -1, and index( ) will raise a ValueError.

Question 5:
Illustrate breaking a comma-separated string into a list of individual tokens.
Answer:
string.split(‘/)

Question 6:
How is a slice of a string specified?
Answer:
Using the colon in the form of start: end index. 2:5 would specify the third through sixth characters in a string.

Question 7:
Given a multiple-line string, how is a list of individual lines obtained?
Answer:
string. splitlines( ) Line breaks are removed unless split lines are called with (True).

Question 8:
What method is used to determine if a string contains any special characters or punctuation?
Answer:
string.alnum( ) If it returns true, there are no special characters in the string.

String manipulation interview questions

Question 9:
What method is used to determine if a string contains only numbers?
Answer:
string.isdigit( ) Any other values in the string will cause this to return False.

Question 10:
What is the purpose of the count!) string method?
Answer:
The count method counts the number of occurrences of a substring within the string. Not the number of characters in the string.

Question 11:
What does the partition( ) string method do?
Answer:
Partition breaks a string into two parts and returns a tuple containing the substring before the separator, the separator itself, and the substring after the separator.

Question 12:
In the string “Fred Flintstone drinks at a foo. A fluorometer measures pressure.” How would you change every occurrence of foo with bar?
Answer:
string.replace^foo’/bar’)

Question 13:
Illustrate converting tabs to 12 spaces each in a string.
Answer:
string.expand tabs(12)

Question 14:
Given a list of words, combine them into one string.
Answer:
myString = “.join(wordlist)

Question 15:
How is the center string method used?
Answer:
It positions a string centered in a field of a specified width, padded by spaces on either side.

Question 16:
How can code inside a string be executed?
Answer:
exec(string) The string is interpreted as Python code.

Question 17:
How are string templates created?
Answer:
By using the $ prefix on variable names within the template string.

Question 18:
Illustrate creating a template string, and then printing it with a variable substituted.
Answer:
import string
myTemp late = string.Template! “A template… The variable = $v”)
print myTemplate.substitute(v=”my variable”)

Question 19:
How is a Unicode string specified?
Answer:
By adding a ‘u’ before the string definition

Question 20:
How is Unicode converted to a local string?
Answer:
Using the string’s encode method. An example might be string.encode(‘utf-8′)