Here is where the error happened
print(f'Hey, {Username}, your password is {'*' * Password}. The password is **{len(Password)}** long.')
asked Mar 2, 2021 at 12:37
2
That is because you are using single quote for main string as well as '*'
.
Python interprets it as —
print( f'Hey, {Username}, your password is {'# string ends here.
*' * Password}. The password is **{len(Password)}** long.')
The last bracket goes unmatched and hence the error.
A quick fix would be to use double quotes in one of the strings.
print(f'Hey, {Username}, your password is {"*" * Password}. The password is **{len(Password)}** long.')
answered Mar 2, 2021 at 12:43
AvyChannaAvyChanna
3365 silver badges12 bronze badges
Hello,
When using the f string on the Question 4, I keep getting this error message in Spyder: SyntaxError: f-string: expecting ‘}’
I have used your code exact as you have in the video, and it still produces the same error message:
user_input = (input(‘Please enter a number between 1 and 12:>>’ ))
while (not user_input.isdigit()) or (int(user_input) < 1 or int(user_input) > 12):
print(‘Must be an integer between 1 and 12’)
user_input = input(‘Please make a selection:>> ‘)
user_input = int(user_input)
print(‘============================’)
print()
print(f’This is the {user_input} times table’)
print()
for i in range(1,13):
print(f'{i} x {user_input} = {i=user_input}’)
Error: runfile(‘/Users/new-dawn/spyder-files/For-Loops.py’, wdir=’/Users/new-dawn/spyder-files’)
File «<unknown>», line 53
print(f'{i} x {user_input} = {i=user_input}’)
^
SyntaxError: f-string: expecting ‘}’
Appreciate your help.
Best Regards,
Justin
Skip to content
text = ["this","is","text"]
print(f"hello and {text, end=","}")
Input In [58]
print(f"hello and {text, end=","}")
^
SyntaxError: f-string: expecting '}'
I am trying to remove brackets and commas while using an f string
……………………
>Solution :
It appears you are trying to print the contents of the list after the hardcoded string. So your code is first printing «hello and» and then the string representation of the list ‘text’. In order to print the elements of the list using an f string you can do:
text = ['this','is','text']
print(f"hello and {' '.join(text)}")
Problem Description:
text = ["this","is","text"]
print(f"hello and {text, end=","}")
Input In [58]
print(f"hello and {text, end=","}")
^
SyntaxError: f-string: expecting '}'
I am trying to remove brackets and commas while using an f string
……………………
Solution – 1
It appears you are trying to print the contents of the list after the hardcoded string. So your code is first printing «hello and» and then the string representation of the list ‘text’. In order to print the elements of the list using an f string you can do:
text = ['this','is','text']
print(f"hello and {' '.join(text)}")
It appears that there is a syntax error in your code on this line:
long_name = f"{self.year} {self.make = make} {self.model = model}"
The =
sign is used for variable assignment, but it seems like you are trying to concatenate strings instead. You can fix this error by removing the =
signs and replacing them with commas, like so:
long_name = f"{self.year} {self.make}, {self.model}"
This will create a string that combines the values of self.year
, self.make
, and self.model
, separated by commas.
Also, it’s worth noting that the print(long_name)
statement at the end of the code block will never execute because it is after the return
statement. Any code after return
is unreachable and will not be executed.
I hope this helps! Let me know if you have any other questions.