关于字符串操作的基本 Python 问题:示例:string.lowercase

发布于 2024-11-09 02:51:10 字数 893 浏览 3 评论 0原文

所以我尝试运行它,但它出错了,我需要帮助了解问题所在。我真的很感谢任何帮助,我正在学习麻省理工学院的开放课程编程简介:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千柳 2024-11-16 02:51:10

我对你想要做的事情有点困惑。这似乎是一个相当做作的练习。

  • 如果您只想使用实例方法,words.lower() 将返回字符串 words 的小写版本。

  • 如果您想使用 string.lowercase 实现您自己的小写函数(这只是一个设置为所有小写字母的字符串的常量),那么您需要确保执行在调用之前导入字符串(例如在lower函数示例中)。

  • 如果您执行了导入字符串,您还可以使用string.lower('STRING with CAPS')

编辑:这是一些运行您的函数的代码,向您展示它的工作原理:

import string

def isLower(ch):
    return string.find(string.lowercase, ch) != -1


words = "GreatTimes"

lowercase_letters = ""
for ch in words:
    print ch, '->', isLower(ch)

    if isLower(ch):
        lowercase_letters += ch

print "Lowercase letters were:", lowercase_letters

输出:

G -> False
r -> True
e -> True
a -> True
t -> True
T -> False
i -> True
m -> True
e -> True
s -> True
Lowercase letters were: reatimes

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 string words.

  • 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 an import string before calling it (e.g. in your lower function example).

  • If you did an import string, you could also use string.lower('STRING with CAPS') instead.

Edit: Here's some code that runs your function, to show you it works:

import string

def isLower(ch):
    return string.find(string.lowercase, ch) != -1


words = "GreatTimes"

lowercase_letters = ""
for ch in words:
    print ch, '->', isLower(ch)

    if isLower(ch):
        lowercase_letters += ch

print "Lowercase letters were:", lowercase_letters

Output:

G -> False
r -> True
e -> True
a -> True
t -> True
T -> False
i -> True
m -> True
e -> True
s -> True
Lowercase letters were: reatimes
_畞蕅 2024-11-16 02:51:10

小写是字符串模块中字符串的名称:

<块引用>
<块引用>

导入字符串
字符串.小写
'abcdefghijklmnopqrstuvwxyz'


但您无法通过字符串实例访问它:

<块引用>
<块引用>

word = '这是一个字符串'
单词小写
回溯(最近一次调用最后一次):
文件“”,第 1 行,位于
AttributeError:“str”对象没有属性“lowercase”


另外, find 方法不会执行您的想法 - 检查文档。

艾伦

lowercase is the name of a string in the string module:

import string
string.lowercase
'abcdefghijklmnopqrstuvwxyz'

but you can't access it through a string instance:

word = 'this is a string'
word.lowercase
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'lowercase'

Also, the find method doesn't do what you think -- check the docs.

Allen

稳稳的幸福 2024-11-16 02:51:10

尝试使用“ascii.lowercase”它可以工作

Try to use "ascii.lowercase" it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文