Important datatype conversions

List to string

List in string can be done using the "join()" method.

listx = ["hello","how","are","you"]
listz =['1','2','3','4']

str1 = ' '.join(listx)
str2 = ','.join(listz)
print(str1)
print(str2)

'''
hello how are you
1,2,3,4

'''

String to list

Using the "split()" and "list()" method.

str1 = "hello,how,are,you"
str2 = 'abcde'
listx = str1.split(',')
listz = list(str2)
list1 = [x for x in str2]
print(listx)
print(listz)
print(list1)

'''
['hello', 'how', 'are', 'you']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
'''

Integer to String and list

x = 1442
str1 = str(x)# using str method 
print(str1)

str2 = ','.join(x for x in str1)# using the join method for custom output
print(str2)

list1 = [int(k) for k  in str(x) ]#using list comprehenstion
print(list1)

'''
1442
1,4,4,2
[1, 4, 4, 2]

'''

List with one datatype to other

for example, a list containing string values to a list with int values

listx = ["12","4","2"]
x = list(map(int,listx))
print(x)

'''
[12, 4, 2]

'''