Python Forum

Full Version: Quotes and variables in print statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a basic issue that has given me fits and I think Atom has been making it more complicated by wanting to place additional [single] quotation marks when I type one.

I want the output to be:

"<searchfor>" is not in the file.

searchfor is a string entered by the user.

If I do
print(searchfor,'is not in the file.')
then I don't get quotes around searchfor.

If I do

print('''searchfor,'' is in the file.')

then I get "SyntaxError: EOF while scanning triple-quoted string literal"

Also, I think input and print are different with regard to how many arguments they'll take and that makes it more difficult when trying to intersperse variables with boilerplate text.

When should I use regular quotation marks rather than a single (apostrophe) and how do I use them for formatting things like this?
One way would be
print('"' + searchfor + '" is in the file.')
but the more pythonic way is using python >= 3.6 and f-strings

print(f'"{searchfor}" is in the file.')
On a related note, I just found out I could do this:

print('words is a',type(words),'and it is:', end=' ')
To get:

words is a <class 'list'> and it is: ['but', 'so', 'expensive']

How exactly does the end= ' ' command or syntax work?
The end parameter defaults to '\n', a new line. So print normally adds a new line to the end of the output. Setting it to ' ' instead adds a space to the end of the output.
Thanks!