Question on how to create a function that returns the first two characters of Python!

Asked 2 years ago, Updated 2 years ago, 15 views

How can I make a function that takes the month and returns the first two characters?

def abbreviation():

Should I use len?

>>> abbreviation( 'March')

'Ma'

How can I print it out like this?

python

2022-09-21 11:32

1 Answers

No matter what value comes in, should I return the first two letters?

def abbreviation(month):
    return month[:2]
>>> abbreviation('March')
'Ma'

You can do it like above.

If you want to return the first two characters only if the input value is one of January through December, try putting in a few conditional statements.


2022-09-21 11:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.