1

I have a sample array ['first_name', 'last_name'] as input and would like to have the output as "first_name", "last_name" without any square brackets but need to have the double quotes around the elements. I have tried below but doesn't seem to work. appreciate any inputs on this.

The array is dynamic. Can have any number of elements. The elements need to be enclosed in double quotes each and no square brackets.

      array_list = ['first_name', 'last_name']
      string_list = list(array_list)
      print(string_list)
3
  • Exactly like this "first_name", "last_name" Commented Dec 16, 2022 at 6:45
  • is the list is specific i.e only first name and last name is in the list.. Commented Dec 16, 2022 at 6:45
  • Could be any elements in the list. First name and last name is just an example Commented Dec 16, 2022 at 6:46

7 Answers 7

2
array_list = ['first_name', 'last_name']
for i in array_list:
    print(f' "{i}" ',end=" ".join(","))
Sign up to request clarification or add additional context in comments.

Comments

1

You can add the intended quotation marks, you can do so with f-string

string_list = [f'"{item}"' for item in array_list]
print(", ".join(string_list))

Comments

1
array_list = ['first_name', 'last_name']

print(', '.join(f'"{e}"' for e in array_list))

Output:

"first_name", "last_name"

Comments

1
array_list = ['first_name', 'last_name']
pre_processed = [f'"{item}"' for item in array_list]
string_list = ", ".join(pre_processed)
print(string_list)

Output:

"first_name", "last_name"

Comments

1

you can do like this using list-string conversion...

Code

array_list = str(['first_name', 'last_name',5]).strip('[]')
print(array_list)
#-------OR--------
array_list = ['first_name', 'last_name'] # only string handle 
print(",".join(array_list))

output

'first_name', 'last_name', 5

Comments

0

you can try below to achieve the same.It has for loop to iterate through the array and convert it to a string with double quotes around each element: @Pal1989

array_list = ['first_name', 'last_name']
string_list = ""
for element in array_list:
  string_list += '"' + element + '", '
string_list = string_list[:-2]
print(string_list)

1 Comment

The name string_list is misleading, it is not a list. String concatenation in a loop is inefficient as it requires copying the characters of the two strings into a new string; repeated concatenation results in repeated copying of a growing number of characters each iteration. You should be using str.join() (which doesn’t require stripping the redundant comma) and f-string formatting, which let you avoid the O(N^2) performance cost here.
0

All you really need to do is to join by the separator and put double quotes at front and back:

array_list = ['first_name', 'last_name']
print('"' + '", "'.join(array_list) + '"')
output: "first_name", "last_name"

Remember: when you need to put double quotes in strings, surround with singles: ' " ' - I've left blanks on purpose. And " ' " to have single quotes.

Comments

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.