inpython
value = "我是中国人" # 判断‘中国’是否在value所代指的字符串中。 “中国”是不是value所代指的字符串的子序列。 v1 = "中国" in value # 示例 content = input('请输入内容:') if "退钱" in content: print('包含敏感字符') # 示例 while True: content = input('请输入内容:') if "退钱" in content: print('包含敏感字符') else: print(content) break
not ingit
not 2 > 1 not 2 > 1 # 错误 not 2>1 # 正确内容详细
age = 18
py2code
有int和long索引
整型除法只能保留整数位。要想保留全部,须要先引入ip
from__future__ import division字符串
from __future__ import division v = 9 /2 print(v)
py3input
字符串特有string
公共it
len ,计算长度。 (字符串->计算字符串中的字符个数)io
索引取值(0做为开始)
v = "oldboy" v1 = v[0] # 0 1 2 3 ... 从前向后 v2 = v[-1] # -1 -2 -3 ...从后向前
切片(0做为开始)
v = "oldboy" # v1 = v[2:4] # 2 =< 索引位置 <3 # v2 = v[3:6] # v2 = v[3:-1] # v2 = v[3:] # v2 = v[:-1] # print(v2) # 示例: 取最后两个字符 # data = input('请输入:') # 方式一 # v = data[-2:] # print(v) # 方式二 # total_len = len(data) # v = data[total_len-2:total_len] # print(v)
练习题
""" 需求:让用户输入任意字符串,获取字符串以后并计算其中有多少个数字。 """ """ total = 0 text = input('请输入内容:') # ads2kjf5adja453421sdfsdf index_len = len(text) index = 0 while True: val = text[index] #print(val) # "a" # 判断val是不是数字 # - 是数字:total + 1 # - 不是:继续玩下走,执行下一次循环去检查下一个字符。 flag = val.isdigit() if flag: total = total + 1 # total += 1 if index == index_len - 1: break index += 1 print(total) """
int范围问题
整数除法问题
py2:只能保留整数位。要想保留全部,须要先引入
from__future__ import division
py3:能保留全部