Python 3 中的 string.lower
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16643166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
string.lower in Python 3
提问by Axarydax
I had a working python script, but something must have changed in python 3.
我有一个可以工作的 python 脚本,但在 python 3 中一定有一些变化。
For example if I wanted to convert argument 1 to lowercase:
例如,如果我想将参数 1 转换为小写:
import string
print(string.lower(sys.argv[1]))
It says that 'module' object has no attribute 'lower'- OK, I understand, stringis a module now.
它说'module' object has no attribute 'lower'- 好吧,我明白了,string现在是一个模块。
If I remove the import, and write only string.lower('FOO'), it complains that name 'string' is not defined.
如果我删除导入并只写string.lower('FOO'),它会抱怨name 'string' is not defined.
So what's the correct way to do convert a string to lowercase?
那么将字符串转换为小写的正确方法是什么?
回答by Ashwini Chaudhary
You can use sys.argv[1].lower()
您可以使用 sys.argv[1].lower()
>>> "FOo".lower()
'foo'
lower()is a method of string objects itself.
lower()是字符串对象本身的方法。
stringmodule has been changed in Python 3, it no longer contains the methods related to strobjects, it now only contains the constants mentioned below.
string模块在 Python 3 中已经改变,它不再包含与str对象相关的方法,它现在只包含下面提到的常量。
You can also use str.lower("Mystring")but that's unnecessary here as you can simply use "Mystring".lower().
您也可以使用,str.lower("Mystring")但这在这里是不必要的,因为您可以简单地使用"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']
回答by HennyH
It's strnot string:
它str不是string:
>>> str.lower("HELLO")
'hello'
Hence the reason you get the name 'string' is not defined.error is that there currently exists no variable in scope called string.
因此,您收到name 'string' is not defined.错误的原因是范围内当前不存在名为string.
回答by Felipe Hoffa
The object oriented correct way is:
面向对象的正确方法是:
'FOO'.lower()
In your example:
在你的例子中:
print(sys.argv[1].lower())
回答by pepr
To add, while some people prefer the object-oriented way (calling the method of the str object), some may still prefer the older way -- with functions or operators. It depends also on the problem being solved. (If you do not agree, think for example about (1.5).__add__(3).)
另外,虽然有些人更喜欢面向对象的方式(调用 str 对象的方法),但有些人可能仍然更喜欢旧的方式——使用函数或运算符。这也取决于要解决的问题。(如果您不同意,请考虑例如(1.5).__add__(3)。)
You can easily create your own (simpler) name for the function that you need to make it more readable. You only should think about whether it will be readable (in future for you and now) for everyone:
您可以轻松地为需要使其更具可读性的函数创建自己的(更简单的)名称。你只应该考虑它是否对每个人都可读(将来对你和现在):
>>> lower = str.lower
>>> lower('SoMe CaPiTaL LetTerS to Be LoWeRED')
'some capital letters to be lowered'

