Print String vertically in Python

Asked

Viewed 2,332 times

2

It’s a string-only exercise, I haven’t gotten to the list part yet
What my mistake ?

print("-"*30)
nome = str(input("Digite seu nome: "))
cont = '0'
while cont < nome:
    c = int(cont)
    print(nome[c])
    cont += '1'
  • this would help? or do you need to declare cont being string? print("-"*30) name = str(input("Type your name: ")) cont = 0 tam=Len(name) while cont<tam: c = int(cont) print(name[c]) cont += 1

  • If you help, yes I put as an answer, otherwise it would be only to disturb better answers

  • Great, this is valid... I had forgotten about the feature "Len". Thanks guy

4 answers

2

Replace all your code with that line:

for c in input("Digite seu nome: "): print(c)

Don’t use the while to iterate over a string, the simplest way is for item in String:

1


You are concatenating string instead of summing in its variable cont and wasn’t getting the size of nome

print("-"*30)
nome = str(input("Digite seu nome: "))
cont = 0 # era '0' string
while cont < len(nome): # aki coloquei len(nome) que é o tamanho
    c = int(cont)
    print(nome[c])
    cont += 1 # aki estava '1' string

https://repl.it/repls/GraveCadetblueOutput


As commented by @Andersoncarloswoss, you are using python 3.x then you don’t need to convert the return of input()

# nome = str(input("Digite seu nome: "))
nome = input("Digite seu nome: ") # ficando assim
  • 2

    You don’t need to convert the output from input for str. In Python 3+ will always be str

0

To resolve this issue, simply implement the following code...

palavra = input('Digite uma palavra: ')

for c in palavra:
    print(c)

0

There are some ways to show a string vertical. To complement, it could be done very simply using a for, staying that way:

print("-"*30)

# Utilização com python versão 2.7
for x in str(input("Digite seu nome: ")):
   print(x)

# Utilização com python versão 3+
for x in input("Digite seu nome: "):
   print(x)

This would be a simpler and easier way to implement.

You can read more about iteration of string in Python in: article and article.

  • 1

    You don’t need to convert the output from input for str. In Python 3+ will always be str.

  • True, but since the version was not specified I forced the conversion. I will change to serve for more cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.