0

I have a array something like this

my_array = ['MY_INVOICE', 'YOUR_INVOICE']

and when I convert it to a string using like this

final_string +=  ','.join(my_array)

it is giving something like this

MY_INVOICE,YOUR_INVOICE

while I want something like this

"MY_INVOICE", "YOUR_INVOICE" (like with commas on it) tried few solutions but not working

5
  • MY_INVOICE,YOUR_INVOICE does have a comma. Do you mean quotes which is the " symbol? Commented Feb 14, 2022 at 17:02
  • Your question is confusing. The result that you're getting has commas in it, but it's not what you want because you want a result with commas in it? Commented Feb 14, 2022 at 17:02
  • 1
    Why do you need this? If you're creating a SQL query, use a prepared statement with parameters rather than substituting a string with quotes. If you're creating JSON, use the json module. Commented Feb 14, 2022 at 17:04
  • Do you mean "? These are double quote marks. Commented Feb 14, 2022 at 17:04
  • 1
    I second Barmar's comment. This seems like an XY problem. If you are building a SQL query, DON'T EVER CONCATENATE STRINGS THAT COME FROM USER INPUT because your code becomes vulnerable to injection attacks. Commented Feb 14, 2022 at 17:13

7 Answers 7

2

Here's what I think you're looking for.

my_array = ['MY_INVOICE', 'YOUR_INVOICE']
print('"' + '", "'.join(my_array) + '"')

Result:

"MY_INVOICE", "YOUR_INVOICE"
Sign up to request clarification or add additional context in comments.

3 Comments

In case i want single commas then ?
By "single commas", do you mean the character '? A "comma" refers to ,, which doesn't seem to be what you mean.
@FawadRana If you want ' instead of " change the order '"' to "'" and so, or use scape character as I suggested with `` before the quotation mark
2

May be :

final_string +=  '"' + '", "'.join(my_array) + '"'

Comments

0

You could do something like this:

my_array = ['MY_INVOICE', 'YOUR_INVOICE']
my_array[0] = '"' + my_array[0] # Adding opening double quotes to the first value
my_array[-1] = my_array[-1] + '"' # Adding closing double quotes to the last value
print('", "'.join(my_array)) # Joining with quotes and comma with space

Comments

0

To turn this list…

my_array = ['MY_INVOICE', 'YOUR_INVOICE']

…into this string:

"MY_INVOICE", "YOUR_INVOICE"

You could do this:

new_str = ', '.join(f'"{word}"' for word in my_array)
Explanation:

f'"{word}"' takes word and puts double-quotes around it - if word was Apple it becomes "Apple"

f'"{word}"' for word in my_array does this for each word in my_array

', '.join(...) joins together the pieces with , in between

3 Comments

I think it's not a good idea use a for inside the join, because you're iterating over my_array two times
It's a matter of priority. I don't think iterating over the loop twice is a concern here, and '", "' is the kind of horror that reminds me of when I could only solve problems by making macro-free spreadsheet ;)
don't think f'"{word}"' is a better choice on readability by the trade off
0

I solved your problem by trying to change your code as less as possible. This is my solution which works fine, even though I don't really know why you would do something like this. Anyway that's not my problem.

my_array = ['MY_INVOICE', 'YOUR_INVOICE']
my_array = (f"\"{element}\"" for element in my_array)
final_string = ""
final_string +=  ','.join(my_array)

This is the output:

"MY_INVOICE","YOUR_INVOICE"

Comments

0

Try:


my_array = ['MY_INVOICE', 'YOUR_INVOICE']

final_string = ''

final_string +=  '\"' +'\", \"'.join(my_array) + '\"'

print(final_string)


wich outputs: "MY_INVOICE", "YOUR_INVOICE"

2 Comments

You don't need to escape double quote inside single quotes, or vice versa.
While this works, it feels convoluted to try to shoe horn the quotes and commas together in a join. My suggestion is to first wrap each element in the array with quotes then simply join with just a comma.
-1

while I want something like this

"MY_INVOICE", "YOUR_INVOICE" (like with commas on it)

I think you mean quotes ("), not commas (,) which your original version already has.

You need to add the quotes yourself. One way is with a generator expression:

final_string +=  ', '.join('"' + w + '"' for w in my_array)

Here I add the quotes around each word before joining them together with commas.

2 Comments

while this works it's seems not clear at all, and also is much slower than other solutions, .join() method is relatively fast but you're adding one iteration over all the words in an array and this is both not necesary and slow.
@UlisesBussi That is a good point about efficiency since there are two iterations over the array here. I'm not sure what you mean by "it's not clear". I think it is more clear since it matches how I parse the output: words surrounded by quotes which are in turn separated by commas. The advantage over other solutions is you don't have to figure out how to add the first quote before the first word and the last quote after the last word.

Your Answer

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.