Home > Mobile >  How to select a particular element from a set
How to select a particular element from a set

Time:10-27

For eg. cars = ['audi' , 'bmw' , 'subaru' , 'toyota'] I only want to take 'bmw' from the list and make it all capital or make the first letter of the word capital.

I expect it to come out as: 'BMW' or 'Bmw'

CodePudding user response:

You take an element from the list by addressing the position. In this case, cars[1] (lists start at 0).

CodePudding user response:

You can use the position in the list to access this item.

cars = ['audi' , 'bmw' , 'subaru' , 'toyota']
item = cars[1] # To Access bmw 

CodePudding user response:

You can simply access list by index

cars = ['audi' , 'bmw' , 'subaru' , 'toyota']
print(cars[1]) 

output

bmw

To print all letters capital

print(cars[1].upper())

output#

BMW

To print only first letter capital

print(cars[1].title())

output#

Bmw

CodePudding user response:

cars = ['audi' , 'bmw' , 'subaru' , 'toyota']
cars[1].upper()
#output
BMW
cars[1].capitalize() 

OR

cars[1].title()
#output
Bmw
  • Related