Python Forum
Quotes and variables in print statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Quotes and variables in print statement (/thread-20923.html)



Quotes and variables in print statement - Mark17 - Sep-06-2019

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?


RE: Quotes and variables in print statement - ThomasL - Sep-06-2019

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.')



RE: Quotes and variables in print statement - Mark17 - Sep-13-2019

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?


RE: Quotes and variables in print statement - ichabod801 - Sep-13-2019

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.


RE: Quotes and variables in print statement - Mark17 - Sep-13-2019

Thanks!