kosar_afr
kosar_afr

Reputation: 129

unable to print objects of a python list in separate lines

i want to print contents of a list in separate lines and this is the code

  mylist=[]
  standard=''
  for i in range(8):
      name=input()
      name = name.lower()

  num=0
  for char in name:
      if num==0:
         standard+=char.upper()
      elif num>0:
         standard+=char.lower()
      num+=1
  mylist.append(standard)
  for element in mylist:
    print(element)

i expect the elements of my list print like this for example:

Water
Sky
Cloud

but it happens to be like this:

Water
WaterSky
WaterSkyCloud

Upvotes: 1

Views: 214

Answers (2)

Roshin Raphel
Roshin Raphel

Reputation: 2699

Try this :

mylist=[]
temp = []
for i in range(3):
    name=input()
    name = name.lower()
    temp.append(name)

for name in temp :
    num=0
    standard=''
    for char in name:
        if num==0:
            standard+=char.upper()
        elif num>0:
            standard+=char.lower()
        num+=1
    mylist.append(standard)
    for element in mylist:
        print(element)

You have to empty standard every time.

Upvotes: 1

BlueSheepToken
BlueSheepToken

Reputation: 6099

If you have a list of elements, it is easier to use built-in methods to print on new lines

print(*[x.title() for x in mylist], sep='\n')

print(*args) is in python3 and allows you to print iterable as you want, here with a \n sep. You can also use .title() on string to take the first character as upper and the others as lower

Upvotes: 2

Related Questions