Python Interview Questions
  • Python - RegEx
    1. What module is required to use regular expressions?
    A) regex
    B) re
    C) regx

    Answer: re
    2. What do you use in a regular expression to match any 1 character or space?
    A) .
    B) _
    C) +

    Answer: .
    3. How would you match for rat and cat in a regular expression?
    A) [cr]at
    B) (cr)at
    C) {cr}at

    Answer: [cr]at
    4. How would you match for any words starting with c through f that end with the letters at?
    A) [c + f]at
    B) (c-f)at
    C) [c-f]at

    Answer: [c-f]at
    5. What is used to match any 1 character or space?
    A) +
    B) \
    C) .

    Answer: .
    6. What is used to match any whitespace?
    A) \s
    B) .
    C) _

    Answer: \s
    7. What is used to match anything except a whitespace?
    A) \s
    B) \S
    C) \\

    Answer: \S
    8. What is used to match any single number?
    A) \d
    B) \D
    C) \#

    Answer: \d
    9. What is used to match anything except for a number?
    A) \d
    B) \S
    C) \D

    Answer: \D
    10. What code would match any number 3 in length?
    A) \D{3}
    B) \d3
    C) \d{3}

    Answer: \d{3}
    11. What is used to match any number between 3 and 5 in length?
    A) \d{3,5}
    B) {\d3,5}
    C) \D{3,5}

    Answer: \d{3,5}
    12. What is used to match 1 or more characters?
    A) .
    B) +
    C) \d

    Answer: +
    13. What regular expression would match for both cat and cats?
    A) [cat] + s
    B) [cat] + s?
    C) [cat]*s

    Answer: [cat] + s?
    14. What regex matches for doctor doctors and doctor's?
    A) [doctor] + ['s]*
    B) [doctor's] +?
    C) [doctor]*?

    Answer: [doctor] + ['s]*
    15. What Regex matches everything in this string "Match everything up to @" up to but not including the @?
    A) r"^.*^@"
    B) r"^.*(^@)"
    C) r"^.*[^@]"

    Answer: r"^.*[^@]"
    16. What Regex will get the phone number, but not the area code from this string "My number is 412-555-1212"?
    A) r"412-(.*)"
    B) r"(412)-.*"
    C) r"412?-(.*)"

    Answer: r"412-(.*)"
    17. Get the phone numbers, but not the area codes from this string "412-555-1212 412-555-1213 412-555-1214"?
    A) r"412-(.{8})"
    B)r"412-(.\d)"
    C) r"412-?(.{8})"

    Answer: r"412-(.{8})"
    18. What Regex will return both the 1st 3 and next 4 numbers, but not the area code from this string "My number is 412-555-1212"?
    A) r"(412-)?(.*)-(.*)"
    B) r"412-?[.*]"
    C) r"412-(.*)-(.*)"

    Answer: r"412-(.*)-(.*)"
    19. Use a Regex to grab all letters and numbers of 1 or more separated by a word boundary but don't include it for this string "One two three four"?
    A) r"(\w+?=\b)"
    B) r"\b+(?=\b)"
    C) r"\w+(?=\b)"

    Answer: r"\w+(?=\b)"
    20. How would you match for any 3 letter word ending with "at" except for cat and rat?
    A) [cr]at
    B) (^cr)at
    C) [^cr]at

    Answer: [^cr]at

  • Apply for an intership with Datai Analytics by filling your email, name and phone number