21828

Add double quotes to string in python

Question:

If my input text is

a b c d e f g

and I want my output text to be: (with the double quotes)

"a b c d e f g"

Where do I go after this step:

" ".join([a.strip() for a in b.split("\n") if a])

Answer1:

You have successfully constructed a string without the quotes. So you need to add the double quotes. There are a few different ways to do this in Python:

>>> my_str = " ".join([a.strip() for a in b.split("\n") if a]) >>> print '"' + my_str + '"' #Use single quotes to surround the double quotes "a b c d e f g" >>> print "\"" + my_str + "\"" #Escape the double quotes "a b c d e f g" >>> print '"%s"'%my_str #Use string formatting "a b c d e f g"

Any of these options are valid and idiomatic Python. I might go with the first option myself simply because it's short and clear

Answer2:

'"%s"' % " ".join([a.strip() for a in s.split("\n") if a])

Recommend

  • What is the Performance, Safety, and Alignment of a Data member hidden in an embedded char array in
  • Calling a second level base class constructor
  • Gson serializing Spring beans
  • How to negative match regex in JavaScript string replace? [duplicate]
  • Grails 3 - How to publish to Artifactory
  • Efficiently reading a csv file with windows newline on linux in Python
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • how can I compare dates in array to find the earliest one?
  • Divide a $1 by 3 and adjusting 1 cent
  • Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
  • How do I shift the decimal place in Python?
  • Approximate Order-Preserving Huffman Code
  • Invalid object name 'dbo.Item'
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Tamper-proof configuration files in .NET?
  • Django: Count of Group Elements
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • AES padding and writing the ciphertext to a disk file
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • How to handle AllServersUnavailable Exception
  • Javascript + PHP Encryption with pidCrypt
  • Web-crawler for facebook in python
  • How to get next/previous record number?
  • WOWZA + RTMP + HTML5 Playback?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • trying to dynamically update Highchart column chart but series undefined
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal