代码之家  ›  专栏  ›  技术社区  ›  donprecious iyeritufu

为什么python sting方法(string.isupper)不带括号(string.isupper())[关闭]

  •  -4
  • donprecious iyeritufu  · 技术社区  · 7 年前

    问题是,即使字母是上下两个字母,if语句也会执行其内部代码。我希望它只在字母为大写时执行。

    word = raw_input("enter a word: ");
    newword = ""
    count = 0
    for w in word:         
        if w.isupper: 
            print "checking";        
            newword += w.upper()
            count  += 1      
    print newword
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   lancew    7 年前

    w.isupper()
    

        2
  •  1
  •   psilocybin    7 年前

    您对的调用中缺少括号 isupper()

    查看 str.isupper() Python Docs 供使用。

    word = raw_input("enter a word: ");
    newword = ""
    count = 0
    for letter in word:         
        if letter.isupper(): 
            print "checking";        
            newword += letter.upper()
            count  += 1      
    print newword