首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决Python 3中的'Str is not callable‘错误?

如何解决Python 3中的'Str is not callable‘错误?
EN

Stack Overflow用户
提问于 2018-07-09 02:06:11
回答 2查看 519关注 0票数 -4

任何指导都是值得感谢的。我是编程新手。

问题:'str is not callable.‘’的运行时错误。而且,很可能是语义错误。说明:"if len(s) str 70: TypeError:‘<=’object is not callable“

预期结果:我正在尝试编写一个函数,该函数接受一个字符串,然后打印该字符串,使该字符串的最后一个字母位于显示的第70列。

我尝试过的:在PEP8中运行代码,它不会返回任何语法错误。删除了将str分配给% s的原始行。

Python 3中的代码:

代码语言:javascript
复制
def right_justify(s):
    '''
    (string) -> string
    takes a string named s
    places it in column 70 - len of string
    '''
    s = input("Type in a word: ")
    for len in s:
        if len(s) <= 70:
            len(s) + 70 - len(s)
    return s 

print(right_justify)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-09 02:19:33

如果我理解您的要求,您希望在字符串前面放置足够的空格,以便它的最后一个字符位于第70列。

您不需要遍历字符串。这将实现您所需的功能。注意,没有必要将input放在函数中,因为您不能使用字符串参数调用函数。

代码语言:javascript
复制
def right_justify(s):
    '''
    (string) -> string
    takes a string named s
    places it in column 70 - len of string
    '''
    if len(s) <= 70:
        output = ((70 - len(s)) * " ") + s
        return output

input_string = input("Type in a word: ")
output_string = right_justify(input_string)
print(output_string)
票数 3
EN

Stack Overflow用户

发布于 2018-07-09 02:41:34

最简单的方法是:

代码语言:javascript
复制
def right_justify(s):
    return "%70s" % s
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51234746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档