Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Quotes and variables in print statement
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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Create, assign and print variables in loop steven_tr 10 257 May-28-2020, 04:26 PM
Last Post: ndc85430
  Taking brackets out of list in print statement pythonprogrammer 3 320 Apr-13-2020, 12:25 PM
Last Post: perfringo
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 227 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  printing a list contents without brackets in a print statement paracelx 1 237 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Embedding return in a print statement Tapster 3 262 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Passing print output into another print statement Pleiades 6 406 Sep-08-2019, 02:37 PM
Last Post: Pleiades
  If statement won't print coreywratchford66 1 432 May-15-2019, 12:26 AM
Last Post: ichabod801
  problems with the If statement or is it the variables being used NickIgoe 2 472 Mar-22-2019, 06:34 AM
Last Post: NickIgoe
  Attribute error print statement error jamshaid1997 1 503 Jan-20-2019, 04:02 PM
Last Post: ichabod801
  how to add a coma in print statement of python without preceding space character? brittocj 3 724 Sep-27-2018, 09:03 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)