Bottom Page

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

Quote
#5
Thanks!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Embedding return in a print statement Tapster 3 89 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Passing print output into another print statement Pleiades 6 156 Sep-08-2019, 02:37 PM
Last Post: Pleiades
  If statement won't print coreywratchford66 1 247 May-15-2019, 12:26 AM
Last Post: ichabod801
  problems with the If statement or is it the variables being used NickIgoe 2 287 Mar-22-2019, 06:34 AM
Last Post: NickIgoe
  Attribute error print statement error jamshaid1997 1 348 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 485 Sep-27-2018, 09:03 PM
Last Post: nilamo
  Put Print Statement Wrong Somehow... abrogard 6 814 Aug-14-2018, 08:24 PM
Last Post: abrogard
  How to get number with print statement Rehan11 1 562 Jul-28-2018, 02:01 PM
Last Post: buran
  Issue using print statement in a thread bweiss1258 9 1,288 Jan-16-2018, 02:07 AM
Last Post: bweiss1258
  Outputting a float value in a print() statement RedSkeleton007 2 791 Jan-11-2018, 09:23 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)