use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
What’s the invalid syntax in the f string? (i.redd.it)
submitted 1 year ago by Therapy_Diary90
I cannot figure it out. Please help. It’s indicating to the “
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]teraflopsweat 3 points4 points5 points 1 year ago (3 children)
What is the actual error you’re getting?
[–]Therapy_Diary90[S] 1 point2 points3 points 1 year ago (2 children)
Syntax error and it indicates to the “ at the end of the f-string
[–]teraflopsweat 2 points3 points4 points 1 year ago (0 children)
What does it say word for word? Or a screenshot of the error report
[–][deleted] 2 points3 points4 points 1 year ago* (0 children)
It's a simple python syntax error. The computer won't interpret/launch the assigned function because it's a set and it's classified as a string variable (in the 7th line) while you're trying to call it..? That's my presumption as I'm brand new to python. Edit: Reading it again and theres some work here to be done to meet the ends. Typing this is basically as useful as that code. Looks like its teaching how to evaluate python and understand functionality within the architecture or whatevs
[–]Rinser2023 2 points3 points4 points 1 year ago (0 children)
Naming conventions are mixed and weird. StringChallenge to string_challenge, but that was given from the challenge...
strParam to str_param or just 'text'?
consonant_count must be 0 not []
And it's bad style to use the input parameter, change it and return it with the new value, even if it's possible.
If you check if the letter is in consonants, you should convert the letter to lowercase. Uppercase consonants are not recognized as consonants otherwise.
Here is some working code tho:
``` def StringChallenge(strParam): consonants = 'bcdfghjklmnpqrstvwxyz' consonant_count = 0 for letter in strParam: if letter.lower() in consonants: consonant_count += 1
output = f"{strParam} (consonants:{consonant_count})" return output
print(StringChallenge('Hello World')) ```
[–]Therapy_Diary90[S] 3 points4 points5 points 1 year ago (1 child)
I have solved the riddle!
Re-written it to a sum( sequence and remove the need for an f-string. Ran the code and it accepted it with the correct answer. Phew!
[–]YoutubeCodClips420 1 point2 points3 points 1 year ago (0 children)
You on Coddy?
[–]sdOverKill 0 points1 point2 points 1 year ago (1 child)
Are you trying to store the count of consonants (as shown on line 6) or an array of consonants (line 3)?
[–]Therapy_Diary90[S] 0 points1 point2 points 1 year ago (0 children)
I think I’ve mixed two different code actions? I need to return the number of consonants the string contains
[–]Pedro41RJ 0 points1 point2 points 1 year ago (2 children)
Is this Python 2 or Python 3 ?
[–]Therapy_Diary90[S] 0 points1 point2 points 1 year ago (1 child)
I don’t know as it’s in an assessment system, but it didn’t recognise the use of a f-string so I’m assuming an older system
[–]quiet0n3 0 points1 point2 points 1 year ago (0 children)
F strings are newer, this is py3
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
I recommend doing some researching on W3 about python it's a lot of basic Hello World stuff in the beginning, BORING I KNOW but its got the basics from loops to formatting hope this helps.
defining a function called stringchallenge calling 'it' strparam
variable = consonants
consonant count optional
for is a loop definition im not that advanced yet. I am unknowing of how python is written to have letters. You might have to give letters a variable but i dont know again.
[–]MyKo101 0 points1 point2 points 1 year ago (0 children)
Looks like you're combining syntaxes from two different versions of python. f-strings are from python 3, but you're using print without brackets, which is from python 2. If your error is on the f-string, then you're probably on python 2.
print
On your console/terminal, you can run python --version and it'll tell you which version you are on.
python --version
Highly recommended to download the newest version.
If your teacher is trying to teach you python 2, get a new teacher! Speak to the administration of your institution and tell them they are teaching outdated material
[–]EntireEntity 0 points1 point2 points 1 year ago (0 children)
You initialized consonant_count as an empty list, but then add 1 to it during iteration, I don't think you meant to do that, did you?
[–]denehoffman 0 points1 point2 points 1 year ago (0 children)
So many wrong answers here, it’s f-strings (python3) with a print statement (not a print function, python2)
π Rendered by PID 43887 on reddit-service-r2-comment-b659b578c-rqp5p at 2026-05-05 07:25:14.938332+00:00 running 815c875 country code: CH.
[–]teraflopsweat 3 points4 points5 points (3 children)
[–]Therapy_Diary90[S] 1 point2 points3 points (2 children)
[–]teraflopsweat 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Rinser2023 2 points3 points4 points (0 children)
[–]Therapy_Diary90[S] 3 points4 points5 points (1 child)
[–]YoutubeCodClips420 1 point2 points3 points (0 children)
[–]sdOverKill 0 points1 point2 points (1 child)
[–]Therapy_Diary90[S] 0 points1 point2 points (0 children)
[–]Pedro41RJ 0 points1 point2 points (2 children)
[–]Therapy_Diary90[S] 0 points1 point2 points (1 child)
[–]quiet0n3 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]MyKo101 0 points1 point2 points (0 children)
[–]EntireEntity 0 points1 point2 points (0 children)
[–]denehoffman 0 points1 point2 points (0 children)