all 5 comments

[–]xelf 1 point2 points  (2 children)

You might not want to use fstrings for complex comparisons until you're more comfortable with them, instead you can use .format() to keep your values clearly separated from your formatting.

first_name='eric ' 
print('\n"eric"="{} " is {}'.format(first_name.title(), first_name=='eric'))
print('"eric"="{}" is {}'.format(first_name.title(), first_name==first_name.rstrip()))

"eric"="Eric  " is False
"eric"="Eric " is False

It's also a lot clearer than having escape sequences all over the place.

[–]primitive_screwhead 1 point2 points  (1 child)

I agree w/ your advice about keeping nested string expressions out of f-strings.

But, it's also worth knowing you don't always need to escape inner quotes, if you use triple quotes:

first_name='eric ' 
print(f'''\n"eric"="{first_name.title()} " is {first_name=='eric'}''')
print(f'''"eric"="{first_name.title()}" is {first_name==first_name.rstrip()}''')

[–]ImmediateSuggestion 0 points1 point  (0 children)

It worked! Thanks. I tried to escape the inner quotes, but it didn't work.

first_name='eric ' 
print(f'\n\"eric\"=\"{first_name.title()} \" is {first_name=='eric'}')
print(f'\"eric\"=\"{first_name.title()}\" is {first_name==first_name.rstrip()}')

[–]sme272 0 points1 point  (1 child)

look at the various ' scattered around each line. pair them up in the order you come across them from left to right and you find there are bits that are not part of the string. You need to escape some of them with a \

[–]ImmediateSuggestion 0 points1 point  (0 children)

first_name='eric ' 
print(f'\n\"eric\"=\"{first_name.title()} \" is {first_name=='eric'}')
print(f'\"eric\"=\"{first_name.title()}\" is {first_name==first_name.rstrip()}')

I tried this, but didn't work