关于字符串操作的基本 Python 问题:示例:string.lowercase
所以我尝试运行它,但它出错了,我需要帮助了解问题所在。我真的很感谢任何帮助,我正在学习麻省理工学院的开放课程编程简介:
words="GreatTimes"
words.lowercase
我得到了这个错误:
AttributeError: 'str' object has no attribute 'lowercase'
问题是我正在尝试运行“如何像计算机科学家一样思考”中的示例代码,它仍然给了我错误。
def isLower(ch):
return string.find(string.lowercase, ch) != -1
所以我无法弄清楚语法错误是什么。查看 Python 文档页面 它说:
string.lowercase 包含以下内容的字符串 所有被考虑的字符 小写字母。在大多数系统上 这是字符串 'abcdefghijklmnopqrstuvwxyz'。这 具体值取决于语言环境, 并将在以下时间更新 locale.setlocale() 被调用。
想法?
编辑: 啊,抱歉,我应该说得更清楚。所以我试图编写一段代码来告诉我字符串中的字符是否为小写。我的代码是我对它的实现。虽然示例代码是我用来尝试检查我的语法是否错误以及问题根源的代码。但是,示例代码也会生成错误。简而言之,为什么这段代码不起作用?看起来它应该吐出一串所有小写字母,比如“GreatTimes”中的“reatimes”和/或者你将如何使用 string.lowercase 来制作这个程序?
So I'm trying to run this, but it errors out and I need help understanding what's wrong. I really appreciate any help, I'm taking the MIT opencourseware intro to programming:
words="GreatTimes"
words.lowercase
And I get this error:
AttributeError: 'str' object has no attribute 'lowercase'
The issue is that I'm trying to run example code from "How to think like a computer scientist" and it still gives me the error.
def isLower(ch):
return string.find(string.lowercase, ch) != -1
So I can't figure out what the syntax error is. Looking at the Python doc page it says this:
string.lowercase A string containing
all the characters that are considered
lowercase letters. On most systems
this is the string
'abcdefghijklmnopqrstuvwxyz'. The
specific value is locale-dependent,
and will be updated when
locale.setlocale() is called.
Thoughts?
Edit:
Ah, sorry I should be more clear. So I'm trying to write a piece of code that will tell me whether or not a character in a string is lowercase or not. my code is my implementation of it. while the example code is something i'm using to try to check to see if my syntax is wrong and that is the source of the problem. However, the example code also generates an error. In a nutshell, why doesn't this code work? It seems like it Should spit out a string of all the lowercase letters like "reatimes" from "GreatTimes" And/or how would you use string.lowercase to make this program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对你想要做的事情有点困惑。这似乎是一个相当做作的练习。
如果您只想使用实例方法,
words.lower()
将返回字符串words
的小写版本。如果您想使用
string.lowercase
实现您自己的小写函数(这只是一个设置为所有小写字母的字符串的常量),那么您需要确保执行在调用之前导入字符串
(例如在lower
函数示例中)。如果您执行了
导入字符串
,您还可以使用string.lower('STRING with CAPS')
。编辑:这是一些运行您的函数的代码,向您展示它的工作原理:
输出:
I'm a bit confused by what you are trying to do. This seems like a rather contrived exercise.
If you just want to use an instance method,
words.lower()
will return a lowercase version of the stringwords
.If you want to implement a lowercase function of your own using
string.lowercase
(which is just a constant set to a string of all lowercase letters), then you need to make sure you do animport string
before calling it (e.g. in yourlower
function example).If you did an
import string
, you could also usestring.lower('STRING with CAPS')
instead.Edit: Here's some code that runs your function, to show you it works:
Output:
小写是字符串模块中字符串的名称:
但您无法通过字符串实例访问它:
另外, find 方法不会执行您的想法 - 检查文档。
艾伦
lowercase is the name of a string in the string module:
but you can't access it through a string instance:
Also, the find method doesn't do what you think -- check the docs.
Allen
尝试使用“ascii.lowercase”它可以工作
Try to use "ascii.lowercase" it works