Search code examples
pythonliststrip

Python Strip a list to remove unwanted characters


I have a list in my program called student.
I append to this list a string with the name "Reece".
When I print the variable holding my name it outputs:

Reece

When I print the list which I appended the variable too it outputted:

['Reece']

How can I like strip it to remove these unwanted characters []'

The code I use is this:

name = "Reece"
print name      #Outputs - Reece

student = []
student.append(name)
print student        #Outputs - ["Reece"]

If I then appended another thing:

Class = "Class A"
student.append(Class)
print student         #Outputs - ["Reece", "Class A"]

Solution

  • This should produce your desired output

    print student[0]
    

    The [ ] are printed because student is a list and [ ] is the list representation.

    If you want to print multiple names in a list, you should look at the join method.

    It works like this:

    ", ".join(["Reece", "Higgs"])
    

    Gives:

    Reece, Higgs