简体   繁体   English

无法在单独的行中打印 python 列表的对象

[英]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

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. print(*args)在 python3 中,允许您根据需要打印可迭代的,这里使用 \\n sep。 You can also use .title() on string to take the first character as upper and the others as lower您还可以在字符串上使用.title()将第一个字符作为上,其他作为下

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.每次都必须清空standard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM