简体   繁体   English

如何获得等于一定数量字符的输入?

[英]How do I get an input to equal a certain amount of characters?

Our teacher has told us to make a program that validates a postcode as a Northern Ireland postcode. 我们的老师告诉我们制作一个程序,以将邮政编码验证为北爱尔兰的邮政编码。 That means it must have the letters "BT" in it and must equal 8 characters. 这意味着它必须包含字母“ BT”,并且必须等于8个字符。 In the code below I managed to get the letters part working. 在下面的代码中,我设法使字母部分起作用。 However, he did not go into detail on how to make the input equal 8 characters. 但是,他没有详细说明如何使输入等于8个字符。 He mentioned using .length() and validation(try and except), but I'm unsure how to use .length() to get 8 characters. 他提到使用.length()和validation(尝试和除外),但是我不确定如何使用.length()获得8个字符。 Here's my code: 这是我的代码:

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.

相关问题 如何将字符串中的一定数量的零替换为要求用户输入的数量,从字符串的末尾开始? - 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 set the amount of list elements equal to the number of characters in a string? 在Python中一定数量的字符出现多于一次之后,如何修剪字符串? - How do I trim a string after certain amount of characters appear more then once in Python? 如何使用 python 在字符串中一定数量的字符后插入空格? - How do I insert a space after a certain amount of characters in a string using python? 如何从某个输入中获得某个响应 - How do I get a certain response from a certain input 我如何从列表中随机选择用户要求在 python 中使用输入的次数? - how do i randomly pick characters out of a list the amount of times the user asks for using input in python? 如何检查用户给定输入的字符串是否等于 Python 中的某个字母/单词 - How do I check if a string with a user given input is equal to a certain letter/ word in Python 如何检查输入是否等于某个整数或多个整数? - How do you check to see if an input is equal to a certain integer or integers? 如何在一定时间后停止“input()”function? - How can I stop the 'input()' function after a certain amount of time? 如何从字符串中删除一定数量的字符 - How to remove a certain amount of characters from a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM