Do anyone know why i got this error ? Do i have to import anything in order to use string.lower() & string.upper() ?
sentence = "how are you"
newS = string.lower(sentence)
NameError: name 'string' is not defined Do anyone know why i got this error ? Do i have to import anything in order to use string.lower() & string.upper() ?
sentence = "how are you"
newS = string.lower(sentence)
NameError: name 'string' is not defined Short expert note: the first error happened because there was no name called string in scope. Case-conversion is implemented as methods on the string type (the str object), so calling the method on the value is the normal approach; the string module in modern Python mainly exposes constants and Template, not free lower/upper functions. This is why older examples that import string differ from current idioms. (docs.python.org)
The second error — AttributeError: 'tuple' object has no attribute 'lower' — means the variable being used is a tuple, and tuples do not provide string methods. Common causes: a stray comma when assigning a literal (which makes a single-element tuple), a function that returned multiple values, or unpacking that produced a tuple. Quick, safe checks and a small fix:
# quick checks
print(type(val), repr(val))
# trailing-comma pitfall:
val = "hello", # val is a tuple ('hello',)
# if val is a tuple of strings, join them into one string first
if isinstance(val, tuple):
text = " ".join(val) # join requires all items to be str
else:
text = str(val)
print(text.lower()) Use isinstance() to test types and str.join() to turn a sequence of strings into one string before calling string methods. (docs.python.org)
Extra tips: prefer casefold() for aggressive caseless comparisons, avoid importing the string module just to change case (that was a Python‑2 era pattern), and check for accidental commas or name shadowing (a local variable named string hides an import). If the code comes from old Python 2 examples, expect some legacy differences in the string module compared with Python 3. (docs.python.org)
Acknowledgement: ’s report of the two errors and ’s diagnosis (method vs module, and the tuple possibility) are consistent with the above debugging steps.
Jump to Post— vegaseat 1,735There are two ways to do this ...
# older way, needs to import string module import string sentence = "HOW ARE YOU" newS = string.lower(sentence) print newS # newer way, no import needed newerS = sentence.lower() print newerS
There are two ways to do this ...
# older way, needs to import string module
import string
sentence = "HOW ARE YOU"
newS = string.lower(sentence)
print newS
# newer way, no import needed
newerS = sentence.lower()
print newerS Why i got this error ?
newS = sentence.lower()
AttributeError: 'tuple' object has no attribute 'lower' The only way I can recreate this error is to make variable sentence a tuple ...
#sentence = "HOW ARE YOU"
sentence = 'HOW', 'ARE', 'YOU'
newS = sentence.lower()
print newS Are you using an older version of Python?
Thank You.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.