Jan 18, 2021
Tags: python
Unterminated String Literal in Python: How to Fix It
Fix SyntaxError: unterminated string literal in Python. Learn how to resolve this issue when printing strings and avoid common mistakes in your code.
my_string = 'Hello World'print(f"{my_string}, {5})
The code has a syntax error due to an unterminated string literal and a mismatched closing parenthesis. The corrected code should look like this:
my_string = 'Hello World'
print(f"{my_string}, {5}")
Changes made:
- Added a newline after the string definition (
my_string = 'Hello World'
) to separate it from the print statement. - Closed the f-string with a closing curly brace (
}
) after the{5}
expression.
With these corrections, the code should run without a syntax error.
Comments
-
Danielle Carline
Posted on
Close the string in the print statement.
my_string = 'Hello World'print(f"{my_string}, {5}")