1

This question is different as I did not understand how the other answers were in accordance with my code. I did check the other questions first. I needed something specific to my code. I need .lower() to work for one of my inputs, however I'm not sure where to place it. I'm not acquainted when it comes to functions like these and so I require a little help. A part of my code is...

phone_model = ["s4", "s5", "note", "j5", "s6", "s7", "s6 edge", "s7 edge", "ace", "ace 2"]
model = input("Which model is your Samsung device?")
if model in phone_model:
    problems = input("OK, what is the problem with your device?")

I would like the function to convert the answer to MODEL into lower case please.

A_Man
  • 99
  • 5
  • Where do you typically put `.lower()` for a `str`? – gold_cy Feb 15 '17 at 21:29
  • 8
    Seven downvotes is a bit harsh for a first time poster. The problem description is clear enough "I need .lower() to work for one of my inputs, however I'm not sure where to place it." The OP showed his code and showed that he knew he should be using `str.lower()`. While the answer might seem obvious, it wasn't to the OP. A bad beginner question would show no code and ask how to make something lowercase without researching enough to know that `str.lower()` would be the right way to do it. Let's make this a more welcoming community and be more accepting of new users and beginning coders. – Steven Rumbalski Feb 15 '17 at 21:50
  • First let's talk about how not to use `lower()`. Don't do `lower("RED")` to get `red` because lower is a string method, not a function. Instead do `"RED".lower()`. Methods are accessed by adding a dot to an object reference and then the method name and parenthesis. If you need to pass any arguments they would go in the parenthesis. This also works `color = "RED"` then `color = color.lower()`. Note that I assigned the result of `color.lower()` back to the name `color`. The reason I did this is that Python strings are immutable. Rather than mutating they return a new string. – Steven Rumbalski Feb 15 '17 at 22:01
  • Thanks a lot more man! I really appreciate what you wrote. You made it clearer for me...@StevenRumbalski – A_Man Feb 16 '17 at 14:03

4 Answers4

4

Here:

phone_model = ["s4", "s5", "note", "j5", "s6", "s7", "s6 edge", "s7 edge", "ace", "ace 2"]
model = input("Which model is your Samsung device?").lower()
if model in phone_model:
    problems = input("OK, what is the problem with your device?")
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
3

You will write lower at the end of the input like so:

model = input("Which model is your Samsung device?").lower()

However I would recommend not doing that and instead changing to lowercase in your conditional statement that way you can preserve the original input.

if model.lower() in phone_model:
Kredns
  • 36,461
  • 52
  • 152
  • 203
1

If you just do:

model = input("stuff").lower()

it will automatically cast the input to lower when it is stored in model.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
1

You can convert your intput model to lowercase when checks it whether in phone_model

if model.lower() in phone_model:

In this way you keep the original input model, when debug is needed, you can check what exactly the user inputed

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125