Python Forum
Quotes and variables in print statement
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?
Reply
#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.')
Reply
#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?
Reply
#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
Reply
#5
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print variables in function? samuelbachorik 3 851 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Why does absence of print command outputs quotes in function? Mark17 2 1,341 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Why doesn't this print statement work? stylingpat 10 5,591 Mar-23-2021, 07:54 PM
Last Post: buran
  Two types of single quotes Led_Zeppelin 2 1,863 Mar-15-2021, 07:55 PM
Last Post: BashBedlam
  Print variable values from a list of variables xnightwingx 3 2,573 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 2,747 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,418 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Quotes vs. no quotes around numbers Mark17 6 3,071 Aug-06-2020, 04:13 AM
Last Post: t4keheart
  Create, assign and print variables in loop steven_tr 10 4,239 May-28-2020, 04:26 PM
Last Post: ndc85430
  Taking brackets out of list in print statement pythonprogrammer 3 2,338 Apr-13-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020