Python: How to add single quotes to a long list

python add quotes to string in list
difference between single, double and triple quotes in python
python add single quotes to string
python escape single quote
python remove quotes from list
python 3 single quotes
python triple quote format
python remove single quotes from list

I want to know the quickest way to add single quotes to each element in a Python list created by hand.

When generating a list by hand, I generally start by creating a variable (e.g. my_list), assigning it to list brackets, and then populating the list with elements, surrounded by single quotes:

my_list = []

my_list = [ '1','baz','ctrl','4' ]

I want to know if there is a quicker way to make a list, however. The issue is, I usually finish writing my list, and then go in and add single quotes to every element in the list. That involves too many keystrokes, I think.

A quick but not effective solution on Jupyter NB's is, highlighting your list elements and pressing the single quote on your keyboard. This does not work if you have a list of words that you want to turn to strings, however; Python thinks you are calling variables (e.g. my_list = [1, baz, ctrl, 4 ]) and throws a NameError message. In this example, the list element baz would throw:

NameError: name 'baz' is not defined

I tried this question on SO, but it only works if your list already contains strings: Join a list of strings in python and wrap each string in quotation marks. This question also assumes you are only working with numbers: How to convert list into string with quotes in python.

I am not working on a particular project at the moment. This question is just for educational purposes. Thank you all for your input/shortcuts.

Yeah but then why not:

>>> s = 'a,b,cd,efg'
>>> s.split(',')
['a', 'b', 'cd', 'efg']
>>> 

Then just copy it then paste it in

Or idea from @vash_the_stampede:

>>> s = 'a b cd efg'
>>> s.split()
['a', 'b', 'cd', 'efg']
>>>

Python:: how to put double quotes for values in a list, strip the [] : abc[1:-1]; split on , : .split(','); bracket each element with "" : '"'+x+'"'; join with , : ','.join; add the [] : '['++']'. TL;DR: '['+','.join(['"'+x+'"' for  Python string functions are very popular. There are two ways to represent strings in python. String is enclosed either with single quotes or double quotes. Both the ways (single or double quotes) are correct depending upon the requirement. Sometimes we have to use quotes (single or double quotes) together in the same string, in such cases, we

You can take input as string and split it to list For eg.

eg="This is python program"
print(eg)
eg=eg.split()
print(eg)

This will give output

This is python program

['This', 'is', 'python', 'program']

Hope this helps

How To Format Text in Python 3, Topics we will cover include: quotes, apostrophes, multiple lines, escape you can print strings on multiple lines to make text, especially lengthy text, easier to read. Here is a list of several of the common escape characters:  Quotes are special characters in Python used to represent string data type. If a single or double quote needs to be part of a string, we can do that in a couple of ways. Let us explore that… Use single quotes if you want to add double quotes and visa versa. If you want to be consistent and use only (either single or double) quote to represent

The best way I found was:

>>> f = [10, 20, 30]

>>> new_f = [f'{str(i)}' for i in x]

>>> print(new_f)

['10', '20', '30']

Raspberry Pi For Dummies, Python doesn't mind whether you use double quotes or single quotes around the You can put all list items on one line, but the program is easier to read if you  In Python, double quote and single quote are the same. There's no different between them. And there's no point to replace a single quote with a double quote and vice versa: 2.4.1. String and Bytes literalsIn plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes (").

Math Adventures with Python: An Illustrated Guide to Exploring , Here, we append the Boolean value True to a list containing the numbers 4 and 5​. 'hello'] To add a string, you need to include either double or single quotes  Adding a single quote around a string python. Python 2.X adding single quotes around a string A 70's TV space show where a creature with long tentacles

Single and Double Quotes, String is enclosed either with single quotes or double quotes. Hence both single quote and double quotes depict string in python but it's sometimes our need Python | Ways to print list without quotes · Python | Avoiding quotes while printing  If you need any type of quoting (single, double, and triple for both) you can "combine"(0) the strings: You may also "combine"(0) raw strings with non-raw strings: (0): In fact, the Python parser joins the strings, and it does not create multiple strings.

Mastering Blender, Single or double quotes may be used interchangeably for strings in Python. Lists are represented in Python as ordered collections of entities within square brackets, where each The first method, append(), adds a single element to a list. The reason is that a single quote or double quote itself is a special character we use in our Python program. In many cases, it has been seen that you want to print a string or you want to work with a string.

Comments
  • @Julien I think you deleted it because it can be directly be a list, instead of a string (and if wanted a string do str(string.split(',')))
  • If some IDE could give plugin support will be better, something like emmet, really doubt anyone will use program to generate it when coding.
  • Is there something better about this compared to s = 'a b cd efg', s.split()
  • @vash_the_stampede That'a option too