-1
name = input('Enter your name: ')
print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5])
print(name[6])
print(name[7])
print(name[8])
print(name[9])
print(name[10])
print(name[11])
print(name[12])
print(name[12])
print(name[13])

The problem is that when I run this program and if the letters are less than 13 it gives an error, how can I prevent this?

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51

3 Answers3

1

Use for loop!

for i in name:
  print(i + "\n")
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
Daweed
  • 1,419
  • 1
  • 9
  • 24
1

If you have not learnt loops and still want to print your name one char each line, you can do something like this.

name = input('Enter your name: ')
print (*name,sep='\n')

The *name will send the data to print statement one char at a time. The sep='\n will ensure that the separator is a new line. That way each character will get printed on a new line.

Here's a sample output of the above program.

Enter your name: Shivansh Sahu
S
h
i
v
a
n
s
h
 
S
a
h
u

Congrats on getting started with Python programming. Hope you do well. Good luck and don't hesitate to ask questions. We are here to help you learn.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
0

You can iterate through strings in Python, using a standard for loop. Doing so iterates through each character of the string.

Consider the following:

name = input('Enter your name: ')
for char in name:
    print(char)

This makes no assumption as to the length of the input.

costaparas
  • 5,047
  • 11
  • 16
  • 26