1

I am a beginner to python, and I have a problem when I was trying my new program. I was trying to define a function to lowercase the inputs, but it only work with integers but int with letters, nor letters, here's what i get:

def SomeString(string):
    lowcase = str(string)
    lowcase.lower()
    print lowcase

Only integers work, integers with letters or letters wont work:

>>> SomeString(TEST0110)
SyntaxError: invalid syntax

and

>>> SomeString(TESTString)

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    SomeString(TESTString)
NameError: name 'TESTString' is not defined

I tried not to use function to do this and it worked fine:

>>> String = "TEST0110"
>>> String.lower()
'test0110'

I don't know why it won't work with function, please help.
THanks.

4
  • 1
    You should have lowcase = lowcase.lower() -- string methods return new strings. They don't modify the original string. In fact, there is no way to modify the original string. python strings are "immutable". Commented Jul 6, 2012 at 4:02
  • Not getting your SyntaxError. Make sure you include EVERYTHING. Commented Jul 6, 2012 at 4:05
  • @JoelCornett, see my answer for a possible reason for the SyntaxError Commented Jul 6, 2012 at 4:10
  • 1
    One other nit-pick -- CamelCase is typically reserved for classes. Methods and functions are written in lowercase_with_underscores (by convention) as set up by PEP 8 ( python.org/dev/peps/pep-0008 ). It's just a convention, but it never hurts to start off with good style early on. Commented Jul 6, 2012 at 4:10

2 Answers 2

3

do this SomeString("TEST0110") instead of SomeString(TEST0110)

When you write SomeString(TEST0110) the code assumes TEST0110 is a variable, but in your case it is not

the quotes "" indicate it is a string

the error NameError: name 'TESTString' is not defined means your code is tring to find the variable with name TESTString

the function .lower() returns the output, which your are not capturing...

Sign up to request clarification or add additional context in comments.

2 Comments

This still doesn't explain the SyntaxError ... (which has me completely confused)
@Zaia: you should not be getting any syntax error with this code,,, it will throw a NameError
3

SomeString(TEST0110) isn't a syntax error, It's a NameError if you don't have a variable called TEST0110

SomeString(0110TEST) is a syntax error

This is because it 0110 is a number, but followed by garbage

SomeString("TEST0110") is probably what you mean. It passes a string to the function

If you make sure you always passing a str you don't need to call str()

def SomeString(my_string):
    lowcase = my_string.lower()
    print lowcase

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.