[英]How do I replace a certain amount of zeros in a string with an amount that the user is asked to input, starting from the end of the string?
[英]How do I get an input to equal a certain amount of characters?
我们的老师告诉我们制作一个程序,以将邮政编码验证为北爱尔兰的邮政编码。 这意味着它必须包含字母“ BT”,并且必须等于8个字符。 在下面的代码中,我设法使字母部分起作用。 但是,他没有详细说明如何使输入等于8个字符。 他提到使用.length()和validation(尝试和除外),但是我不确定如何使用.length()获得8个字符。 这是我的代码:
postcode = input("Please enter an Northern Ireland postcode:")
BT = "BT"
while BT not in postcode:
postcode = input("That isn't a Northern Ireland postcode. Try again:")
print("This is a Northern Ireland postcode")
要获取字符串中的字符数,可以使用len(postcode)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.