Search code examples
pythonlistvariables

How to store multiple lines separated by enter value in a single variable in python? Example is in the description


555
554
33
2444
4555
2455
24555
2555
2335
5555
23455
2455
2344

I want to store all these values in the single variable num, how can I do that?


Solution

  • If you are working with a multiple line string you can use split given "\n" as the splitor:

    myString = """
    555
    554
    33
    2444
    4555
    2455
    24555
    2555
    2335
    5555
    23455
    2455
    2344
    """
    myString.strip().split("\n")
    

    Output

    ['555',
     '554',
     '33',
     '2444',
     '4555',
     '2455',
     '24555',
     '2555',
     '2335',
     '5555',
     '23455',
     '2455',
     '2344']
    

    Note that if you are working with file, you can still use the same approach, but you need to read the file first.