top of page
realcode4you

Python Interview Questions and Tutorial-4 | Get Support In Python


1. What are escape characters, and how do you use them?

Ans: Escape characters represent characters in string values that would otherwise be difficult or impossible to type into code.


2. What do the escape characters n and t stand for?

Ans: \n is a newline; \t is a tab.


3. What is the way to include backslash characters in a string?

Ans: The \\ escape character will represent a backslash character.


4. The string "Howl's Moving Castle" is a correct value. Why isn't the single quote character in the word Howl's not escaped a problem?

Ans: The single quote in Howl's is fine because we’ve used double quotes to mark the beginning and end of the string.


5. How do you write a string of newlines if you don't want to use the n character?

Ans: Multiline strings allow you to use newlines in strings without the \n escape character


6. What are the values of the given expressions?

'Hello, world!'[1]

'Hello, world!'[0:5]

'Hello, world!'[:5]

'Hello, world!'[3:]

Ans: The expressions evaluate to the following:

• 'e'

• 'Hello'

• 'Hello'

• 'lo world

7. What are the values of the following expressions?

'Hello'.upper()

'Hello'.upper().isupper()

'Hello'.upper().lower()

Ans: The expressions evaluate to the following:

• 'HELLO'

• True

• 'hello'


8. What are the values of the following expressions?

'Remember, remember, the fifth of July.'.split()

'-'.join('There can only one.'.split())

Ans: 1>[‘Remember,’,’remember,’,’the’,’fifth’,’of’,’july’]

2>’there-can-only-one’


9. What are the methods for right-justifying, left-justifying, and centering a string?

Ans: The rjust(), ljust(), and center() string methods, respectively


10. What is the best way to remove whitespace characters from the start or end?

Ans: The lstrip() and rstrip() methods remove whitespace from the left and right ends of a string, respectively


11. What is the name of the feature responsible for generating Regex objects?

Ans: The re.compile() function returns Regex object


12. Why do raw strings often appear in Regex objects?

Ans: Raw strings are used so that backslashes do not have to be escaped


13. What is the return value of the search() method?

Ans: The search() method returns Match objects


14. From a Match item, how do you get the actual strings that match the pattern?

Ans: The group() method returns strings of the matched text.


15. In the regex which created from the r'(\d\d\d)-(\d\d\d-\d\d\d\d)', what does group zero cover? Group 2? Group 1?

Ans: Group 0 is the entire match, group 1 covers the first set of parentheses, and group 2 covers the second set of parentheses


16. In standard expression syntax, parentheses and intervals have distinct meanings. How can you tell a regex that you want it to fit real parentheses and periods?

Ans: Periods and parentheses can be escaped with a backslash: \., \(, and \).


17. The findall() method returns a string list or a list of string tuples. What causes it to return one of the two options?

Ans: If the regex has no groups, a list of strings is returned. If the regex has groups, a list of tuples of strings is returned.


18. In standard expressions, what does the | character mean?

Ans: The | character signifies matching “either, or” between two groups.


19. In regular expressions, what does the character stand for?

Ans: which character ?not mentioned


20. In regular expressions, what is the difference between the + and * characters?

Ans: The + matches one or more. The * matches zero or more.


21. What is the difference between {4} and {4,5} in regular expression?

Ans: The {4} matches exactly three instances of the preceding group. The {4,5} matches between four and five instances.


22. What do you mean by the \d, \w, and \s shorthand character classes signify in regular expressions?

Ans: The \d, \w, and \s shorthand character classes match a single digit, word, or space character, respectively.


23. What do means by \D, \W, and \S shorthand character classes signify in regular expressions?

Ans: The \D, \W, and \S shorthand character classes match a single character that is not a digit, word, or space character, respectively.


24. What is the difference between .* and .*?

Ans: The .* performs a greedy match, and the .*? performs a nongreedy match.


25. What is the syntax for matching both numbers and lowercase letters with a character class?

Ans: Either [0-9a-z] or [a-z0-9]


26. What is the procedure for making a normal expression in regax case insensitive?

Ans: Passing re.I or re.IGNORECASE as the second argument to re.compile() will make the matching case insensitive.


27. What does the . character normally match? What does it match if re.DOTALL is passed as 2nd argument in re.compile()?

Ans: The . character normally matches any character except the newline character. If re.DOTALL is passed as the second argument to re.compile(), then the dot will also match newline characters.


28. If numReg = re.compile(r'\d+'), what will numRegex.sub('X', '11 drummers, 10 pipers, five rings, 4 hen') return?

Ans: 'X drummers, X pipers, five rings, X hens'


29. What does passing re.VERBOSE as the 2nd argument to re.compile() allow to do?

Ans: The re.VERBOSE argument allows you to add whitespace and comments to the string passed to re.compile()


30. How would you write a regex that match a number with comma for every three digits? It must match the given following:

'42'

'1,234'

'6,368,745'

but not the following:

'12,34,567' (which has only two digits between the commas)

'1234' (which lacks commas)

Ans: e.compile(r'^\d{1,3}(,{3})*$') will create this regex, but other regex strings can produce a similar regular expression.


21. How would you write a regex that matches the full name of someone whose last name is Watanabe? You can assume that the first name that comes before it will always be one word that begins with a capital letter. The regex must match the following:

'Haruto Watanabe'

'Alice Watanabe'

'RoboCop Watanabe'

but not the following:

'haruto Watanabe' (where the first name is not capitalized)

'Mr. Watanabe' (where the preceding word has a nonletter character)

'Watanabe' (which has no first name)

'Haruto watanabe' (where Watanabe is not capitalized)

Ans: re.compile(r'[A-Z][a-z]*\sWatanabe')


22. How would you write a regex that matches a sentence where the first word is either Alice, Bob, or Carol; the second word is either eats, pets, or throws; the third word is apples, cats, or baseballs; and the sentence ends with a period? This regex should be case-insensitive. It must match the following:

'Alice eats apples.'

'Bob pets cats.'

'Carol throws baseballs.'

'Alice throws Apples.'

'BOB EATS CATS.'

but not the following:

'RoboCop eats apples.'

'ALICE THROWS FOOTBALLS.'

'Carol eats 7 cats.'

Ans: re.compile(r'(Alice|Bob|Carol)\s(eats|pets|throws)\ s(apples|cats|baseballs)\.', re.IGNORECASE)



If you have any query or need any help in python then send your requirement details at:



or write your query in below comment section

Comments


bottom of page