2.Python (运算符,字符串)

1.运算符  

     a.结果是值   

                    算术运算   a=10*10

                    赋值运算   a=a+1

     b.结果是布尔值

                   比较运算     a=1>5

                   逻辑运算     a=1>6 or 1==1

                   成员运算     a="XX" in "AA" 

1 name = "郑建文"
2 
3 if "建文" in name:
4      print('OK')
5 else:
6      print('Error')

2.基础数据类型

 a.数字

(1)int  字符串转换为数字

         base=16 将数字用十六进制表示为十进制

num = "0011" 
v = int(num, base=16)
print(v)

         

num ='A'
v = int(num, base=16)
print(v)

    (2) bit_lenght  当前数字的二进制,至少用n位表示

b.字符串

(1) .capitalize() 首字母大写

  (2)    .casefold()  /  .lower()  所有变小写

 (3).center(a,b) 长度为a    b可有可无,但是只能是一个字符,有的话用于空白填充

test='yuyu'
v=test.center(20,"*")
print(v)

 (4).count('a',b,c) 去字符串中寻找,寻找子序列'a'出现的次数,b和c是数字可有可无,表示在b-c范围之间查找

(5).endswith(格式同上) 判断字符串是否以指定字符或子字符串结尾

         .startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。

(6).expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 4

     为了分割字符串

test='sjahm\tajajjj\taaaaaa\nsjahm\tajajjj\taaaaaa\nsjahm\tajajjj\taaaaaa\n'
v=test.expandtabs(6)
print(v)

(7).find('字符串')从开始往后找,找到第一个之后,获取其位置,找不到返回-1

         .index('字符串')找不到报错

(8)格式化,将一个字符串中的占位符替换为指定的值

test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v)
test = 'i am {name}, age {a}’
 v = test.format_map({"name": 'alex', "a": 19})

(9).isalnum() 字符串中是否只包含 字母和数字,返回布尔值

        .isalpha()  是否是字母,汉字 返回布尔值

(10)当前输入是否是数字,输入必须是字符串类型,不能是int型,返回布尔值

          一,.isdecimal()只能判别阿拉伯数字

          二,isdigit()能判断阿拉伯数字和带圈圈的数字②

          三,isnumeric()上面干的它都能干,,还能判断中文一二三

(11).isprintable()是否存在不可显示的字符比如\t制表符,\n换行符

(12).isspace()判断是否全部是空格

(13)判断是否是标题

test = "Return True if all cased characters in S are uppercase and there "
v1 = test.istitle()  #判断是否是标题
print(v1)
v2 = test.title()    #转换为标题格式
print(v2)
v3 = v2.istitle()
print(v3)

 (14)☆☆☆☆☆☆将字符串中的每一个元素按照指定分隔符进行拼接 

test = "你是风儿我是沙"
print(test)
v = "_".join(test)
print(v)

(15)判断是否是大小写  test.islower()   test.isupper()

           转换成大小写 test.lower()    test.upper()

           大小写转换   test.swapcase()

(16)移除指定字符串

        test.strip()头尾包含有指定字符序列中的字符或空格就删除

        test.lstrip()用于截掉字符串左边的空格或指定字符  有限最多匹配

        test.rstrip()用于截掉字符串左边的空格或指定字符

test = "jkkjhgkjkjl"
v = test.lstrip('jk')  
print(v)

(17)对应关系替换

test ="aeiou"
test1 = "12345"

v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")
new_v = v.translate(m)
print(new_v)

 (18)分割成三部分

test = "testasdsddfg"
v = test.partition('s')  #从左开始分割成三部分,以s为中心
print(v)
v1 = test.rpartition('s')  #从右开始分割成三部分,以s为中心
print(v1)

 (19)从左分割成指定个数 test.split('s',指定个数)

             从右分割成指定个数 test.rsplit('s',指定个数)

(20)分割,只能根据换行符分割,true,false决定是否保留换行符

test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(False)
print(v)

 (21)是否是有效标识符 test.isidentifier()

(22)将指定字符串替换为指定字符串

test = "alexalexalex"
v1 = test.replace("ex",'bbb')
print(v1)
v = test.replace("ex",'bbb',3)  #替换前三个
print(v)

 (23)创建一串数字,用只有for循环才能全部表示出来

r1=range(1,10,2)
print(r1)
for item in r1:
    print(item)

 (24)根据用户输入的值,输出每一个字符以及当前字符所在的索引位置

test=input("请输入")
l=len(test)
r=range(0,l)
for item in r:
    print(item,test[item])

 (25)字符串合并

name = "zhengjianwen"
age = "18"
info = name + age
print(info)

练习题

1.利用下划线将列表的每一个元素拼接成字符串,li=['alex','eric','rain']

我写的

li=['alex','eric','rain']
a=''
l=len(li)
v=range(l)
for test in v:
    a=a+'_'+li[test]
print(a)

 2.计算输入的内容中有几个十进制数字?几个字母?

我写的

content=input('请输入')
l=len(content)
r=range(0,l)
zimu=0
num=0
for item in r:
    if content[item].isalpha():
        zimu=zimu+1
    elif content[item].isdigit():
        num=num+1
print(zimu,num)

3.

.

我写的

def check_code():
    import random
    checkcode=''
    for i in range(4):
        current=random.randrange(0,4)
        if current !=i:
            temp=chr(random.randint(65,90))
        else:
            temp=random.randint(0,9)
        checkcode+=str(temp)
    return checkcode
while 1==1:
    code = check_code()
    print(code)
    cc = input('请输入验证码')
    if cc==code:
        print('right')
        break

4.

.

name = input('name')
place = input('place')
habit = input('habit')
template = "{0}喜欢在{1}{2}".format(name,place,habit)
print(template)

5

value = input('请输入')
v1, v2 = value.split("+")
v1 = int(v1)
v2 = int(v2)
print(v1+v2)

 

 

 

 

 

 
posted @ 2020-02-20 19:27  最近饭吃的很多  阅读(246)  评论(0编辑  收藏  举报