在开发应用程序时,有许多情况需要我们对字符串进行操作并将其转换为可变的数据结构,例如列表。
# Importing ast library import ast # Initialization of strings str1 ="'Python', 'for', 'fun'" str2 ="'vishesh', 'ved'" str3 ="'Programmer'" # Initialization of list list = [] # Extending into single list for x in (str1, str2, str3): list.extend(ast.literal_eval(x)) # printing output print(list) # using eval # Initialization of strings str1 ="['python, 'for', ''fun']" str2 ="['vishesh', 'ved']" str3 ="['programmer']" out = [str1, str2, str3] out = eval('+'.join(out)) # printing output print(out)