Which of the following are valid ways to specify the string literal foo'bar in Python:
1 'foo\'bar'
2 """foo'bar"""
3. foo'bar
4. none of above
Answers
Answered by
23
Option B is the correct answer:
Python raw string is created by prefixing a string literal with 'r' or 'R'.
Python raw string treats backslash (\) as a literal character.
This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.
Answered by
1
Answer:
Option 1 'foo\'bar' is the correct answer.
Explanation:
Using a backslash before a single quotation instructs Python to end the string. so when we used "foo'bar" in the print statement, it won't end and the output will be foo'bar.
- By adding 'r' or 'R' to a string literal in Python, a raw string is formed.
- Backslash (\) is treated as a literal character in Python raw string.
- When we want a string with a backslash in it but don't want it to be considered as an escape character
- In a raw string, it is escaped when a backslash is followed by a quote. The backslash is still present in the outcome.
- This feature prevents us from producing a raw string of just backslashes.
- Additionally, a raw string cannot end with an odd number of backslashes.
#SPJ3
Similar questions