str = "卡巴斯基#杀毒软件#免费版#俄罗斯#"
#截取到-1,-1不取
str = str[:-1]
#按照#分割
print(str.split("#"))
去最后一个#,然后再按照#分割,输出结果为:
['卡巴斯基', '杀毒软件', '免费版', '俄罗斯']
str = "那车水马龙的人世间,那样地来 那样地去,太匆忙"
index = str.rfind("那")
print(index)
变量名.rfind()为从右侧开始查找,输出结果为:
15
str = input("请输入一个字符串:")
#判断后三个字符串是否为.py
if str[-3:] == ".py":
print(True)
else:
print(False)
或者使用:
str = input("请输入一个字符串:")
print(str.endswith(".py"))
进行判断。
4. 有一身份证号,判断此为男还是女,基于此方法,写一个算法,判断一个身份证号为男还是女。(身份证分15位和18位)
def is_man(idcard):
if len(idcard) == 18:
return True if int(idcard[-2])%2 != 0 else False
if len(idcard) == 15:
return True if int(idcard[-1])%2 != 0 else False
b = is_man("111111111111111111")
print(b)
str = "name-age-sex-address"
print(str.split("-"))
s = "apple is a apple."
list = [str(i) for i in s]
list.sort()
count = 0
a = list[0]
for item in list:
if item == a:
count += 1
else:
print(a,":",count)
count = 1
a = item
print(a,":",count)
str = " fgh "
print(str.rstrip())
#print(str.repalce(" ",""))
str = "hello"
print(str)
print(str[::-1])
#拼接
print(str + str[::-1])
#或者
import operator
print(operator.concat(str,str[::-1]))
s = "hello word"
s1 = s[-3:]+s[:-3]
print(s1)
s = "qwewq"
print(s[::-1] == s)
s = "01#张三#20-02#李四#30-03#王五#40"
list1 = s.split("-")
list2 = [item.split("#") for item in list1]
sum = 0
for item in list2:
sum += int(item[2])
print(sum)