11python学习记录3 字符串

字符串  str

###########################################

# test = "aLex"
# 1 首字母大写  v = test.capitalize()
# 2 所有变小写,casefold更牛逼,很多未知的对相应变小写
# v1 = test.casefold()
# v2 = test.lower()

# 3 设置宽度,并将内容居中  v = test.center(20,"中")

内容居左 v = test.ljust(20,"*")    ;       内容居右 v = test.rjust(20,"*")

# 20 代指总长度
# v = test.center(20,"中")
# print(v)

# 4 去字符串中指定位置之间寻找子序列的出现次数   # v = test.count('ex',5,6)
# 欠
# encode
# decode

# 5
# 以什么什么结尾# 以什么什么开始       v = test.endswith('ex')   v = test.startswith('ex')





***# 6 多用列表格,找到每一个\t 然后默认以空格补全对应设置的位置个数,断句20为例  v = test.expandtabs(20)
 test = "username\temail\tpassword\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123"
# v = test.expandtabs(20)
# print(v)
# 7获取设置字符串第一个出现的其位置     # v = test.find('ex')

# 8 index找不到,报错   忽略   用少 v = test.index('8')

# 9 格式化,将一个字符串中的占位符替换为指定的值  要有{ }         .format
# test = 'i am {name}, age {a}'
# print(test)
# v = test.format(name='alex',a=19)
# print(v)
也可以默认对应的
# test = 'i am {0}, age {1}'
# print(test)
# v = test.format('alex',19)
# print(v)

# 10 格式化,传入的值 {"name": 'alex', "a": 19}
# test = 'i am {name}, age {a}'
# v1 = test.format(name='df',a=10)
# v2 = test.format_map({"name": 'alex', "a": 19})


# 11 字符串中是否只包含 字母和数字 v = test.isalnum()



# 12 是否是字母,汉字  v = test.isalpha()





# 13 当前输入是否是数字
# test = "二" # 1,②
# v1 = test.isdecimal()      2
# v2 = test.isdigit()        2  ②
# v3 = test.isnumeric()      2  ② 二
# print(v1,v2,v3)




# 14 是否存在不可显示的字符 v = test.isprintable()
# \t   制表符
# \n   换行
# test = "oiuas\tdfkj"
# v = test.isprintable()
# print(v)

# 15 判断是否全部是空格 v = test.isspace()
# test = ""
# v = test.isspace()
# print(v)


# 16 判断是否是标题   v1 = test.istitle()
# test = "Return True if all cased characters in S are uppercase and there is"
# v1 = test.istitle()
# print(v1)
# v2 = test.title()     将首字母全变大写,变成标题形式
# print(v2)
# v3 = v2.istitle()
# print(v3)


# 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接   v = "_".join(test)


# 18 判断是否全部是大小写 和 转换为大小写
# v1 = test.islower()
# v2 = test.lower()

# v1 = test.isupper()
# v2 = test.upper()

# print(v1,v2)

# 19        ???  这个有点难理解有什么用啊
# 移除指定字符串
# 优先移除最多匹配

# test = "xa"
# # v = test.lstrip('xa')        
# v = test.rstrip('9lexxexa')
# # v = test.strip('xa')
# print(v)

# 去除左右空白  # 去除\t \n        # v = test.strip()
# v = test.lstrip()
# v = test.rstrip()

# v = test.strip()

# 20 对应关系替换
# test =  "aeiou"
# test1 = "12345"
# v = "asidufkasd;fiuadkf;adfkjalsdjf"
# m = str.maketrans("aeiou", "12345")
# new_v = v.translate(m)
# print(new_v)
# 21 分割为三部分 # 左开始,  找到第一个s分割成三份        v = test.partition('s') 
# v = test.partition('s')     #左开始,找到第一个s分割成三份
# v = test.rpartition('s')    #右
# 22 分割为指定个数 但是自己的s分隔符找不到了  # v = test.split('s',2)
# v = test.split('s',2)
# test.rsplit()

# 23 分割,只能根据换行\n分割,true,false:控制是否保留换行\n
# test = "asdfadfasdf\nasdfasdf\nadfasdf"
# v = test.splitlines(False)
# print(v)
#  24 判断是否以xxx开头# v = test.startswith('a'),以xx结尾     # test.endswith('a)

# 25 大小写转换      v = test.swapcase()

# 26 判断是否字母,数字,下划线   # v = a.isidentifier(): 标识符 def  class

                                               可以用来  写出标识位置 

i=0
test =input("shu ru")
for item in test :
    if False ==item.isidentifier() :
        print(item,i)

    i+=1                                         

# 27 将指定字符串替换为指定字符串
# test = "alexalexalex"
# v = test.replace("ex",'bbb')
# print(v)
# v = test.replace("ex",'bbb',2)
# print(v)
###################### 7个基本常用的函数######################
# join       # '_'.join("asdfasdf")       
# split
# find
# strip
# upper
# lower
# replace

###################### 4个基本操作######################
# test = "郑建文妹子有种冲我来"


# 一、for循环
# for 变量名 in 字符串:
#     变量名
# break
# continue


# index = 0
# while index < len(test):
#     v = test[index]
#     print(v)
#
#     index += 1
# print('=======')


# for zjw in test:
#     print(zjw)


# test = "郑建文妹子有种冲我来"            
# for item in test:
#     print(item)
#     break


# for item in test:
#     continue
#     print(item)


# 二、索引,下标,获取字符串中的某一个字符
# v = test[3]
# print(v)


# 三、切片
# v = test[0:1] # 0=<  <1
# v = test[0:-1] # 0=<  <倒数一个
# print(v)


# 四、获取长度
# Python3: len获取当前字符串中由几个字符组成
# v = len(test)
# print(v)
aa=[11,55,66,3]获取列表中按照,分割的个数
v2 = len(aa)


# 注意:其他数据结构都能用
# len("asdf")
# for循环
# 索引
# 切片


# 五、获取连续或不连续的数字,
# Python2中直接创建在内容中
# python3中只有for循环时,才一个一个创建
# r1 = range(10)
# r2 = range(1,10)
# r3 = range(1,10,2)
# 帮助创建连续的数字,通过设置步长来指定不连续
# v = range(0, 100, 5)
# for item in v:
#     print(item)


##### 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 #####
# test = input(">>>")
# for item in test:
#     print(item)


# 将文字 对应的索引打印出来:
# test = input(">>>")
# print(test)   # test = qwe   test[0]   test[1]
# l = len(test) # l = 3
# print(l)
#
# r = range(0,l) # 0,3
# for item in r:
#     print(item, test[item]) # 0 q,1 w,2 e


# test = input(">>>")
# for item in range(0, len(test)):
#     print(item, t;;;;est[item])


# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串


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



列表   list
...
元祖   tuple
...
字典   dict
...

布尔值 bool
...

----> 列表,元祖,字典,布尔值 详细操作见课上代码
































 

猜你喜欢

转载自blog.csdn.net/yunyupianlan/article/details/80880235