Program to print first character of elements inside a list

In this program, we will print first character of each element inside a list

Create a list of city names

city = ['Paris','New York','London','Tokyo']

Finding number of elements inside a list using len() function

l = len(city)
print('Number of elements in a list',l)

Creating a variable i and assigning 0 value to it

i = 0

Now using while loop to print first character from each element inside a list

while i < l:
print(city[i][0])
i+=1

Output

Number of elements in a list 4
P
N
L
T

Complete Code

city = ['Paris','New York','London','Tokyo']
l = len(city)
print('Number of elements in a list',l)
i = 0
while i < l:
print(city[i][0])
i+=1
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s