Python程序检查字符串是否为pangram
在本教程中,我们将编写一个程序来检查字符串是否为字符集。让我们从讨论pangram开始。
什么是潘格拉姆?
如果一个字符串包含所有字母(无论是小写还是大写),则该字符串称为panagram。
我们可以通过不同的方式实现目标。让我们在本教程中看看其中两个。
1.一般
尝试使用以下步骤编写程序。
算法
1. Import the string module.
2. Initialize a variable with ascii_lowercase string. string.ascii_lowercase Contains all the
alphabets as a string.
3. Initialize the string which we have to check for pangram.
4. Define a function called is_anagram(string, alphabets).
4.1. Loop over the alphabets.
4.1.1. If the character from alphabets is not in the string.
4.1.1.1. Return False
4.2. Return True
5. Print pangram if the returned value is true else print not pangram.示例
## importing string module
import string
## function to check for the panagram
def is_panagram(string, alphabets):
## looping over the alphabets
for char in alphabets:
## if char is not present in string
if char not in string.lower():
## returning false
return False
return True
## initializing alphabets variable
alphabets = string.ascii_lowercase
## initializing strings
string_one = "The Quick Brown Fox Jumps Over The Lazy Dog"
string_two = "TutorialsPoint TutorialsPoint"
print("Panagram") if is_panagram(string_one, alphabets) else print("Not Panagram")
print("Panagram") if is_panagram(string_two, alphabets) else print("Not Panagram")输出结果
如果运行上面的程序,您将得到以下结果。
Panagram Not Panagram
2.使用集合
让我们看看如何使用set数据结构获得相同的结果。请参阅以下步骤以了解想法。
算法
1. Import the string module. 2. Initialize a variable with ascii_lowercase string. string.ascii_lowercase contains all the alphabets as a string. 3. Initialize the string which we have to check for pangram. 4. Convert both alphabets and string(lower) to sets. 5. Print pangram if string set is greater than or equal to alphabets set else print not pangram.
让我们编写代码。
示例
## importing string module
import string
## initializing alphabets variable
alphabets = string.ascii_lowercase
## initializing strings
string_one = "The Quick Brown Fox Jumps Over The Lazy Dog"
string_two = "TutorialsPoint TutorialsPoint"
print("Panagram") if set(string_one.lower()) >= set(alphabets) else print("Not Pana gram")
print("Panagram") if set(string_two.lower()) >= set(alphabets) else print("Not Pana gram")输出结果
如果运行上面的程序,您将得到以下结果。
Panagram Not Panagram
结论
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短