How do I turn a list into a string [duplicate] - python

This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts

You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])

Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])

Related

This python string sorting works but it shouldn't. Should it? [duplicate]

This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 2 years ago.
Taking an intro to python course, the following code sorts the string words appropriately, but the function does not return sortString, so I'm not understanding why the output is correct. Can you please help me understand?
def sort_words(string):
splitString = string.split()
sortString = splitString.sort()
return splitString
print(sort_words('python is pretty cool'))
Python .sort() returns None, much like print() does. list.sort() works in place - meaning the list is sorted without needing to assign it to another variable name.

From string to fstring [duplicate]

This question already has answers here:
Transform string to f-string
(5 answers)
Closed 2 years ago.
I'm trying to pull a string from JSON, then convert it to an f string to be used dynamically.
Example
Assigned from JSON I get
whose_fault= "{name} started this whole mess"
How to build a lambda to convert it to an f-string and insert the given variable? I just can't quite get my head around it.
I know similar questions have been asked, but no answer seems to quite work for this.
Better question. What's the most pythonic way to insert a variable into a string (which cannot be initially created as an f-string)?
My goal would be a lambda function if possible.
The point being to insert the same variable into whatever string is given where indicated said string.
There is no such thing as f-string type object in python. Its just a feature to allow you execute a code and format a string.
So if you have a variable x= 2020, then you can create another string that contains the variable x in it. Like
y = f"It is now {x+1}". Now y is a string, not a new object type,not a function

String format: getting u'' inside the final string [duplicate]

This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')

Replacing multiple strings in Python [duplicate]

This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I currently have the below Python code which works well:-
caseURL = r"\\mydomain\abc\lp_t\GB\123456\Original Format"
caseURL = caseURL.replace("lp_t", "lp_i")
caseURL = caseURL.replace("Original Format", "1")
This works fine as said and carries out the below conversion:-
\\mydomain\abc\lp_t\GB\123456\Original Format
\\mydomain\abc\lp_i\GB\123456\1\
This however just seems a bit clumsy. Is there a more pythonesque way to perform these two segment replacements?
Thanks
A similar post already exists:
How to replace multiple substrings of a string?
You can pick one answer from multiple options in the above post.

How to split a string in Python that has on its own 2 arguments [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
It's really odd but I have this string:
"['please', 'help']"
I want something that would get one argument at a time.
I've searched everywhere for this but I didn't find anything.
Thanks in advance
While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):
import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Are you looking for something like this?
string = "['please', 'help']"
string_list = eval(string)
print string_list[0], string_list[1]
Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.

Categories

Resources