简体   繁体   English

python确保单词大写

[英]python make sure that word is capitalized

I'm trying to make sure that every word that is typed is uppercased as in ABC.我试图确保输入的每个单词像 ABC 一样大写。 The code I'm working with is this我正在使用的代码是这样的

abbreviate=input("Abbreviation: ")
words=abbreviate[0:6]
numbers = abbreviate[6:8]
if len(abbreviate)==9:
    if words.isalpha: #I think it's also possible to use.if str(words)?
        if words.isupper: #(i did upper first, what is the difference?)
            if int(numbers):
                print("correct")
            else:
                 print("wrong, you need numbers")
        else:
             print("wrong, all words are supposed to be uppercase")
    else:
         print("wrong, it needs to be words(alphabet)") 
else:
     print("wrong, the length needs to be 8")    

QWERTY567 should be correct. QWERTY567 应该是正确的。 qwerty567 should be incorrect. qwerty567 应该是不正确的。 How do I go on doing this?我该如何继续这样做?

qwerty333 012345678 qwerty333 012345678

str.isalpha and str.isupper are methods . str.isalphastr.isupper方法 You need to call them to get a result:你需要打电话给他们以获得结果:

if words.isalpha():
    if words.isupper():

About your comments关于您的评论

I think it's also possible to use if str(words) ?我认为也可以使用if str(words)

No, str(words) would do nothing.不, str(words)什么也不做。input() returns a string, so words is already a string.input()返回一个字符串,所以words已经是一个字符串。

I did upper first, what is the difference?我先做upper ,有什么区别?

str.upper converts a string to uppercase, eg 'a'.upper() == 'A' str.upper字符串转换为大写,例如'a'.upper() == 'A'


By the way顺便一提

int() doesn't return a Boolean. int()不返回布尔值。 It might be better to use str.isnumeric to check if numbers is numeric instead.最好使用str.isnumeric来检查numbers是否为数字。

It's simpler to use guard clauses than nested conditionals:使用保护子句比嵌套条件更简单:

if len(abbreviate) != 9:
    print("wrong, the length needs to be 9")
elif not words.isalpha():
    print("wrong, the abbreviation must be alphabetic")
...
else:
    print("correct")

IIUC In order to just verify, whether first 5 characters are uppercase letters, and next 4 are digits, you can do: IIUC为了只是验证前5个字符是否是大写字母,后4个是数字,你可以这样做:

import re

abbreviate=input("Abbreviation: ")

if(re.match(r"^[A-Z]{5}\d{4}", abbreviate)):
    print("correct!")
else:
    print("wrong!")

If moreover you want to ensure it's only 9 characters in total (ie input consists of exactly 5 uppercase letters first, then 4 digits) you can do:此外,如果您想确保它总共只有 9 个字符(即输入正好包含 5 个大写字母,然后是 4 个数字),您可以执行以下操作:

import re

abbreviate=input("Abbreviation: ")

if(re.match(r"^[A-Z]{5}\d{4}$", abbreviate)):
    print("correct!")
else:
    print("wrong!")

Use a regular expression:使用正则表达式:

^[A-Z0-9]+$

See a demo on regex101.com .在 regex101.com 上查看演示

If you have all letters and then at a certain index, you have all numbers, I would suggest:如果你有所有字母,然后在某个索引处,你有所有数字,我建议:

abbreviation=input("Abbreviation: ")
i = len(abbreviation)
for n, char in enumerate(abbreviation):
    if char.isalpha() and char.isupper():
        pass
    else:
        i = n
        break
for char in abbreviation[i:]:
    if char.isnumeric():
        pass
    else:
        print('Wrong')

https://www.geeksforgeeks.org/python-string-isnumeric-application/ https://www.geeksforgeeks.org/python-string-isnumeric-application/

https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/ https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/

You find the first non-lowercase letter, and then everything after it should be all numeric.您找到第一个非小写字母,然后它之后的所有内容都应该是数字。 If you need at least 1 uppercase letter, you can also modify that.如果您需要至少 1 个大写字母,您也可以修改它。 Please tell me if you have questions!如果您有任何问题,请告诉我!

如果 abbreviate 的值为 'QWERTY567',那么您应该使用words = abbreviate[0:6]numbers = abbreviate[6:8]isalpha()isupper()来编辑您的代码以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM