Python中的字符串.标点符号
在 Python3 中, string.punctuation
是一个预初始化的字符串,用作字符串常量。在Python中, string.punctuation
将给出所有的标点符号集。
Syntax : string.punctuation
Parameters : Doesn’t take any parameter, since it’s not a function.
Returns : Return all sets of punctuation.
注意:确保导入字符串库函数以便使用string.punctuation
代码#1:
# import string library function
import string
# Storing the sets of punctuation in variable result
result = string.punctuation
# Printing the punctuation values
print(result)
输出 :
!"#$%&'()*+, -./:;<=>?@[\]^_`{|}~
代码#2:给定标点符号测试。
# import string library function
import string
# An input string.
Sentence = "Hey, Geeks !, How are you?"
for i in Sentence:
# checking whether the char is punctuation.
if i in string.punctuation:
# Printing the punctuation values
print("Punctuation: " + i)
输出:
Punctuation:,
Punctuation: !
Punctuation:,
Punctuation: ?