Home > Back-end >  I use \n for a new line inside arrays. But it didn't work. (python)
I use \n for a new line inside arrays. But it didn't work. (python)

Time:10-13

why doesn't \n work in arrays? Here, the \n does not create a newline. Python makes \n as a string. the result, the words don't create a new line down. i actually know, we can use "Print".But i'm just curious. This is my code: (Thank you)

def Europe():

    country = [
       "England\nFrance\nGermany",
       "Poland\nSpain\nItaly",
       "Finland\nSweden\nNorway"
    ]

print(country) 

Europe()

CodePudding user response:

For some reason Python does not interpret \n when you print an array as a whole, so you must iterate through all the elements. Also fixed some syntax errors.

def Europe():
    
    country = [
        "England\nFrance\nGermany",
        "Poland\nSpain\nItaly",
        "Finland\nSweden\nNorway"]

    for countries in country:
        print(countries)

Europe()

CodePudding user response:

You failed to call your function Europe(). A for loop would have done a great help.

CodePudding user response:

country = [ "England\nFrance\nGermany", "Poland\nSpain\nItaly", "Finland\nSweden\nNorway ]

print(country)

  • Related