【问题标题】:How to calculate the length of the string in python如何在python中计算字符串的长度
【发布时间】:2018-01-30 22:21:36
【问题描述】:

您好,我是一名学生,我的代码有一些错误,谁能帮助我。 问题是输入单词列表和整数,如果单词的长度大于整数,则返回单词。 这是我的回答。

def filter_long_words(string):
string = raw_input("Enter a words : ")
n = raw_input("Enter an integer : ")

count = 0
for letter in string:
    count = count + 1
print "The total string are : ", count

return count 

filter_long_words(string)
if count > n:
    print string

【问题讨论】:

  • 你得到什么输出
  • 读取整数时,转换raw_input的返回值。要获取字符串的长度,请使用len(),并使用==与整数进行比较
  • count 会给你字符串中的字符数而不是单词
  • 你是指单词还是单词?
  • @Haris 你说的 'raw_input' 的返回是什么意思?对不起,我的 python 很差

标签: python function string-length


【解决方案1】:

在不计算空格的情况下求字符串的长度

def StrLx_(my_str):
    #I am defining a function here for it if you don't want it you can 
    #delete this part
      
    space = " "  
    no_space = ""  
    a = 0

    for char in my_str:
        if char not in space:
            no_space = no_space + char

    string = no_space

    while string[a] != string[-1]:
        a = a+1
    
    print(a+1)

计算空格的字符串长度

def StrL_(my_str):
    #lly here

    a = 0
    
    while my_str[0] != my_str[-1]:
        a = a+1
    
    print(a+1) 

【讨论】:

    【解决方案2】:

    你可以用len()获取任意字符串的长度

    示例:

    print (len("string"))
    

    结果:

    6
    

    这是一个简单的例子:

    在您的问题中,您表示该指令是:

    如果单词的长度大于整数,则返回单词。

    下面的代码会做到这一点:

    my_str = raw_input("Enter a word: ")
    n = raw_input("Enter an integer: ")
    
    def filter_long_words(my_str, n):
        if len(my_str) > int(n):
            return my_str # assigns the results of my_string to some_word
    
    some_word = filter_long_words(my_str, n)
    
    print some_word
    

    在 cmets 中回答您的问题:

    def filter_long_words():
        my_str = raw_input("Enter a word: ")
        n = raw_input("Enter an integer: ")
        if len(my_str) > int(n):
            return my_str # assigns the results of my_string to some_word
    
    some_word = filter_long_words()
    
    print some_word
    

    最后一个例子。假设您将几个单词作为一个大字符串输入。

    我们可以使用.split()来获取每个单词并分别测试。

    # simulates raw input of 4 words being typed in at once.
    list_of_words_as_a_string = "One Two Three Four"
    n = raw_input("Enter an integer: ")
    
    def filter_long_words(word, n):
        if len(word) > int(n):
            print word
            return word # this is only useful if you are doing something with the returned word.
    
    for word in list_of_words_as_a_string.split():
        filter_long_words(word, n)
    

    使用 3 作为整数时的结果:

    Enter an integer: 3
    Three
    Four
    

    【讨论】:

    • 所以输入的字符串和整数不应该包含在函数定义中?
    • 如果你愿意,他们可以。您可以将 raw_input 移动到函数的顶部并从函数中删除参数。
    • @Asyiqin 我在函数内部添加了第二个带有raw_input 的示例
    • 不要调用你的变量str
    • @ason​​gtoruin 你是对的。我已经在我的示例中修复了它。
    【解决方案3】:

    你可以使用len函数来获取字符串的长度。

    def filter_long_words():
      string = raw_input("Enter a words : ")
      n = raw_input("Enter an integer : ")
    
      print ("The total string are : ", len(string))
      if len(string) > int(n):
        return string
    

    【讨论】:

    • 您的缩进已关闭。
    • 如果以后将string 作为用户输入,那么使用它有什么意义?
    • 我的意图是要求使用len 函数来获取字符串长度。
    • 您不需要删除答案中的整个代码。您可以将len() 的使用转移到比较两个用户输入的代码部分中。并修复了缩进问题。
    【解决方案4】:

    我不确定我是否理解您是否需要检查一个单词或几个单词的长度,并且只保留足够长的单词。既然别人回答了一个字,我就回答几个字:

    string = raw_input("Enter several words: ")
    n = int(raw_input("Enter an integer: "))
    
    
    def filter_long_words(string, n):
        long_words = [word for word in string.split() if len(word) > n]
        return long_words
    
    print filter_long_words(string, n)
    

    所以,如果string = 'one two three four five six'n = 3,输出将是['three', 'four', 'five']

    【讨论】:

    • 是的,这正是要走的路。 split 是这里的选项。
    • 这可能是 OP 想要的。我想enter a words 把我们都吓跑了。
    • @SierraMountainTech 抱歉,我的错误应该是enter a list of words。拆分的作用是什么?
    • @Asyiqin split() 方法返回一个由空格分隔的字符串中所有单词的列表。您可以在此处查看文档:tutorialspoint.com/python/string_split.htm
    【解决方案5】:

    你可以通过len()获取字符串的长度

    string = raw_input("Enter a words : ")
    n = int(raw_input("Enter an integer : ")) # convert the input to integer
    
    if len(string) > n :
        print(string)
    

    【讨论】:

    • 您的示例很好,但是如果字符串的 len 大于给定的 int,则指示 OP“返回”该单词。所以这个例子应该是我认为的一个函数。
    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多