How to process capitalisation in words
Question:
I tried to solve this problem but don’t know how to, I would appreciate it if you could help me.
criteria:
If the paint colour label contains the string ‘blue’ with any capitalisation, your program should say: ‘Excellent! We’ll soon have enough paint for the sky!’
I need to know how how to make the code register the capitalisation of the word ‘blue’
colour = input ('Which colour did you find? ')
number_of_tins = int(input('Number of tins: '))
if 'blue' in colour:
print ("Excellent! We'll soon have enough paint for the sky!")
thank you
Answers:
You can just convert the user input into all lowercase when checking in the if statement.
if 'blue' in colour.lower()
The colour.lower()
makes all the characters in colour lowercase, so you don’t really have to complicate it.
Strings have a few methods to help you out, namely lower
and upper
. Typically lower
is used in cases like this. It returns a new string of each letter in lowercase or uppercase respectively. So you can simply compare ‘blue’ to colour.lower()
In the input you could use methods like colour = input ('Which colour did you find? ').lower()
it would convert all your input into lower case
even user type in capital and your program works
I tried to solve this problem but don’t know how to, I would appreciate it if you could help me.
criteria:
If the paint colour label contains the string ‘blue’ with any capitalisation, your program should say: ‘Excellent! We’ll soon have enough paint for the sky!’
I need to know how how to make the code register the capitalisation of the word ‘blue’
colour = input ('Which colour did you find? ')
number_of_tins = int(input('Number of tins: '))
if 'blue' in colour:
print ("Excellent! We'll soon have enough paint for the sky!")
thank you
You can just convert the user input into all lowercase when checking in the if statement.
if 'blue' in colour.lower()
The colour.lower()
makes all the characters in colour lowercase, so you don’t really have to complicate it.
Strings have a few methods to help you out, namely lower
and upper
. Typically lower
is used in cases like this. It returns a new string of each letter in lowercase or uppercase respectively. So you can simply compare ‘blue’ to colour.lower()
In the input you could use methods like colour = input ('Which colour did you find? ').lower()
it would convert all your input into lower case
even user type in capital and your program works