0

hi,

i'm having trouble taking say the input of the user and converting it to the right type of format i need in order to compare them.

An example is say I have the number 1234 and that is assigned to the variable x and the string value of that is assigned to y. How would i go from this string to one which looked like ?

I thought i would iterate over the string and append each value, however i was unsuccessful with this code.

a=[]
x=1234
y=str(x)
for i in y:
    a.append(str(y))

The reason i am doing this is so i can compare this to another value with a simliar format, as well as a side function to this i would like to convert this format back to just a string however for that part I am lost.

cheers for any help you can give me

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by vegaseat
0
x = 1234
y = str(x)
a = list(y)
print ', '.join(repr(var) for var in (x,y,a))

Edited by pyTony: n/a

0

thanks, alot but is there no way to go from that format back to the original of just a single string?

0
x = 1234
y = str(x)
a = list(y)
astring = ''.join(a)
print ', '.join(repr(var) for var in (x,y,a,astring))
0

Correcting your original thought ...

a = []
x = 1234
y = str(x)
for character in y:
    a.append(character)

print(a)  # ['1', '2', '3', '4']
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.