Convert a string array to a array [duplicate] - python

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 11 months ago.
Say the string is like a = "['Hello', 'World']". After the conversion, a = ['Hello', 'World'] and the type is a list.

It's called expression evaluation. Read about Python's eval() and literal_eval().
Notice that it may be dangerous, so read the docs carefully.

Related

Is there a way for python to take in a string with math operators and convert it to an expression? [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 1 year ago.
I am trying to take a piece of user input: 5+5 or 5*5 but my code receives it as a string. I want to convert it to an expression from which an answer can be deduced. However, I am not sure how to do this.
For that there's a standard function called as eval.
Examples:
>>> eval("5*5")
25
>>> eval("5+5")
10
>>> eval("2+3*4-4")
10
It will take a string and then calculate the output as per the BODMAS Rule.
Simply use "eval", but unsafe.

Convert python string "['a', 'b']" to list [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a form that save lists as strings, I want to convert those strings to lists before save in DB.
I'm using python3 and Django.
The strings looks like:
'["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]'
what will be the most pythonic way to do that?
import json
json.loads('["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]')

Python: user definied number format in string [duplicate]

This question already has answers here:
Slicing strings in str.format
(6 answers)
Closed 6 years ago.
May be a question that has already been asked:
How can I print an integer in a user definied format.
For example
input = 123456
print(".....".format(input))
where the output should be:
"ab123.45.6"
Unfortunately, slicing in string formating is not possible. So something like:
"ab{0[:2]}.{0[3:4]}.{0[5]}".format(str(input))
would not work.
But is there a better solution than:
"ab{0[0]}{0[1]}{0[2]}.{0[3]}{0[4]}.{0[5]}".format(str(input))
Why not make the slices format() arguments? It looks a bit cleaner.
user_input = str(123456)
print("ab{0}.{1}.{2}".format(user_input[:3], user_input[3:5], user_input[5]))

String in to list Python [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
I have a string, something like:
a = "[1, 2, 3]"
If there an easy way to convert it in to a list, without using .split(), join() etc.
Thanks for any replies.
use ast.literal_eval() it's safer than using eval
from ast import literal_eval
a = literal_eval("[1, 2, 3]")
print(a)

How to reverse a string in python [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
How do I reverse each word in a string, but in the same sentence order? [duplicate]
(1 answer)
Closed 6 years ago.
How to reverse string in python? my input string is "Hey Gun" now i want to display "Gun Hey" as output. I have tried using slice operator like [::-1] but it won't shows proper output how it works in python?
Do splitting and then reversing and then joining.
' '.join(string.split()[::-1])
Using split and reversed (a bit slower though):
>>> a
'Hey Gun'
>>> ' '.join(reversed(a.split()))
'Gun Hey'

Categories

Resources