0

I am trying to format a string in python that takes arguments as items from a list of names. The catch is, I want to print all the list items with double quotes and backslash and one after each other in the same string only. The code is:

list_names=['Alex', 'John', 'Joseph J']
String_to_pring='Hi my name is (\\"%s\\")'%(list_names)

The output should look like this:

'Hi my name is (\"Alex\",\"John\",\"Joseph J\")'

But instead, I keep getting like this:

'Hi my names is (\"['Alex','John','Joseph J']\")'

I've even tried using .format() and json.dumps() but still the same result.

Is there any way to print the desired output or can I only print each list item at a time?

Taku
34.1k12 gold badges79 silver badges88 bronze badges
asked Apr 8, 2018 at 6:52

3 Answers 3

2

Without changing much of your code, you could simply format the repr representation of the list that's converted into a tuple.

# proper way - this is what you actually want
list_names = ['Alex', 'John', 'Joseph J']
string_to_print = 'Hi my name is %s' % (repr(tuple(list_names)))
 
print(string_to_print)
# Hi my name is ('Alex', 'John', 'Joseph J')

If you want to get your exact output, just do some string replacing:

# improper way
list_names = ['Alex', 'John', 'Joseph J']
string_to_print = 'Hi my name is %s' % (repr(tuple(list_names)).replace("\'", '\\"'))
print(string_to_print)
# Hi my name is (\"Alex\", \"John\", \"Joseph J\")

if you're trying to pass string_to_print to some other place, just try the proper way first, it might actually work for you.


If you were mindful enough, you'll find that the previous "improper way" contains a small bug, try this adding "Alex's house" into list_names, the output would look like this:

Hi my name is (\"Alex\", \"John\", \"Joseph J\", "Alex\"s house")

To take care of that bug, you'll need to have a better way of replacing, by using re.sub().

from re import sub
list_names = ['Alex', 'John', 'Joseph J', "Alex's house"]
string_to_print = 'Hi my name is %s' % (sub(r'([\'\"])(.*?)(?!\\1円)(1円)', r'\"2円\"', repr(tuple(list_names))))
print(string_to_print)

But if things like this wouldn't happen during your usage, I would suggest to keep using the "improper way" as it's a lot simpler.

answered Apr 8, 2018 at 7:05
Sign up to request clarification or add additional context in comments.

5 Comments

I tried using the improper way and it really works. Why we are passing tuple as list names and not the actual lists?
Lists are formatted with square brackets, but your expected output uses parentheses instead, which represents tuple. You can remove tuple(), but the output would change to "Hi my name is [\"Alex\", ...]"
With the replace function added after the tuple_names, the output I'm getting is: Hi my name is (\\"Alex\\", \\"John\\", \\"Joseph J\\"). The backslash is getting replaced with double backslashed and not the single.
You're not using print(), what you're seeing is just the repr of tuple_names, it's for showing you that the double quotes are escaped. The actual string contains only one backslash. If you ran my code as-is, or write it to a file, you'll see that there's only one backslash.
Didn't consider the "Alex's House" scenario as I was just trying to print the first names only. But thanks for the upgrade.
0

There is no function for formatting lists as human-friendly strings You have to format lists yourself:

names = ",".join(r'\"{}\"'.format(name) for name in list_names)
print(names)
#\"Alex\",\"John\",\"Joseph J\"
print('Hi my name is ({})'.format(names))
#Hi my name is (\"Alex\",\"John\",\"Joseph J\")
answered Apr 8, 2018 at 6:57

3 Comments

To the downvoter: what's wrong with my suggested solution?
Your method works as well, thanks. Is it possible to follow same approach by passing arguments to %s as well?
%s is an old-style formatting mechanism and should be avoided pyformat.info
0

This is one way using format and join:

list_names = ['Alex', 'John', 'Joseph J']
String_to_pring='Hi my name is (\\"{}\\")'.format('\\",\\"'.join(i for i in list_names))
# Hi my name is (\"Alex\",\"John\",\"Joseph J\")
answered Apr 8, 2018 at 6:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.