相关文章推荐
愤怒的大象  ·  python - Tkinter ...·  1 年前    · 
奔跑的炒面  ·  vue3babel-loader、cache ...·  1 年前    · 
玉树临风的炒粉  ·  c# - Using ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How do I get this code to work in 3?

Please note that I am not asking about "foo".upper() at the string instance level.

import string
    print("string module, upper function:")
    print(string.upper)
    foo = string.upper("Foo")
    print("foo:%s" % (foo))
except (Exception,) as e:
    raise

output on 2:

string module, upper function:
<function upper at 0x10baad848>
foo:FOO

output on 3:

string module, upper function:
Traceback (most recent call last):
  File "dummytst223.py", line 70, in <module>
    test_string_upper()
  File "dummytst223.py", line 63, in test_string_upper
    print(string.upper)
AttributeError: module 'string' has no attribute 'upper'

help(string) wasn't very helpful either. Far as I can tell, the only function left is string.capwords.

Note: a bit hacky, but here's a my short-term workaround.

import string
    _ = string.upper
except (AttributeError,) as e:
    def upper(s):
        return s.upper()
    string.upper = upper
                docs.python.org/2/library/…: "You should consider these functions as deprecated, although they will not be removed until Python 3."  Use the string instance methods.
– David Maze
                Jul 25, 2018 at 23:18

All of the string module-level functions you describe were removed in Python 3. The Python 2 string module documentation contains this note:

You should consider these functions as deprecated, although they will not be removed until Python 3.

If you have string.upper(foo) in Python 2, you need to convert that to foo.upper() in Python 3.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.