string.lower in Python 3 -


i had working python script, must have changed in python 3.

for example if wanted convert argument 1 lowercase:

import string print(string.lower(sys.argv[1])) 

it says 'module' object has no attribute 'lower' - ok, understand, string module now.

if remove import, , write string.lower('foo'), complains name 'string' not defined.

so what's correct way convert string lowercase?

you can use sys.argv[1].lower()

>>> "foo".lower() 'foo' 

lower() method of string objects itself.

string module has been changed in python 3, no longer contains methods related str objects, contains constants mentioned below.

you can use str.lower("mystring") that's unnecessary here can use "mystring".lower().

>>> import string  # python 3 >>> dir(string) ['chainmap', 'formatter', 'template', '_templatemetaclass', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace'] 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -