Python - 检查分隔符是否相等
如果需要检查字符串的分隔符是否相等,则使用“len”方法、“list”方法和“set”运算符以及“if”条件。
示例
以下是演示:
my_string = '96%96%96%96%96%96'
print("The string is : " )
print(my_string)
my_split_char = "%"
print("The character on which the string should be split is :")
print(my_split_char)
my_result = len(list(set(my_string.split(my_split_char)))) == 1
print("The resultant list is : ")
if(my_result == True):
print("All the splits are equal")
else:
print("All the splits are not equal")输出
The string is : 96%96%96%96%96%96 The character on which the string should be split is : % The resultant list is : All the splits are equal
解释
定义了一个字符串并显示在控制台上。
定义了字符串应按其进行分割的字符。
它也显示在控制台上。
字符串按此字符进行分割,并转换为一个集合以获取唯一元素。
它被转换为一个列表。
检查它是否长度为 1。
如果是,则此布尔值存储在一个变量中。
根据该布尔变量的值,在控制台上显示相关消息。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP