欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python第二天---字符串的魔法

程序员文章站 2023-11-12 09:27:10
# "adcbdefg" # "a" # 判断某个东西是否在里面包含 in | not in # name = "abcdefg" # # if "adc" in name: # print("ok") # else : # print("EROOR") # ctrl + ? 是注释 # 布尔值 真... ......
# "adcbdefg"
# "a"

# 判断某个东西是否在里面包含  in  | not in

# name = "abcdefg"
#
# if "adc" in name:
#     print("ok")
# else :
#     print("eroor")

# ctrl + ? 是注释

# 布尔值   真  true 假  false
"""
比较运算符
==
>
<
>=
<=
!=  <>   不等于


逻辑运算符
and
or
not  取反
多运算需加()
执行顺序
    从前到后
    true   or  ==>  true
    true   and ==>  go on
    false  or  ==>  go on
    false  and ==>  false

成员运算
in
not in

"""

"""
赋值运算和算数运算   一样
    a = a + 1      a += 1
    
    +   +=
    -   -=
    *   *=
    /   /=
    %   %=
    **  **=
    //  //=
"""

"""
基本数据类型
数字
    int 整形(3)
    long 长整形(2)
    
    - int 
        将字符串转变为数字
            a = "123"
            print(a, type(a))
            b = int(a)
            print(type(b),b)
            
            num = "a"
            v = int(num,base=16)
            print(v)
    -bit_length       
            age = 3
            r = age.bit_length()
            print(r)
字符串     str

列表      list

元组      tuple

字典      dict

布尔值     bool

"""

# 字符串的魔法

test = "cuihaicheng"
v1 = test.capitalize()    # 首字母大写,其他小写
print(v1)

v2 = test.casefold()      # 所有变小写,他更牛逼
v3 = test.lower()         # 所有变小写
print(v2)
print(v3)

v4 = test.center(20, "-")   # 设置宽度,并将内容剧中,20为总长度,-为空白处为知填充,一个字符可有可无
print(v4)

v25 = test.ljust(20, "-")   # 设置宽度,并将内容剧左,20为总长度,-为空白处为知填充,一个字符可有可无
v26 = test.rjust(20,'-')    # 设置宽度,并将内容剧右,20为总长度,-为空白处为知填充,一个字符可有可无
v27 = test.zfill(20)
print(v25, '\n',  v26, '\n', v27)

v5 = test.count("i", 3, 8)    # 统计i的个数,3为从第几个开始,8为第几个结束
print(v5)

v6 = test.endswith("g")     # 以什么结尾
v7 = test.startswith("c")   # 以什么开始
print(v6, v7)

v8 = test.find("ai", 3, 8)     # 从开始往后找,找到第一个后,获取其位置    3,8 表示大于等于3小于8
print(v8)

text = "i am {name},age {a}"
print(text)

v9 = text.format(name='cuihaicheng', a=24)      # 格式化
v10 = text.format_map({'name':'cuihaicheng', 'a':'24'}) # 格式化,字典形式
print(v9)
print(v10)

v11 = test.index('i')   # index找不到,报错,忽略不用
print(v11)

test1 = 'dsjadas234234+'
test2 = 'sdhfasf3249873294'
v12 = test1.isalnum()       # 字符串中是否只包含字母和数字
v13 = test2.isalnum()
print(v12)
print(v13)

v15 = test2.isalpha()       # 只包含字符和汉字
print(v15)

test3 = 'username\temail\tpassword\ncuiahicheng\t132@qq.com\thuawei12#$\ncuiahicheng\t132@qq.com\thuawei12#$\ncuiahicheng\t132@qq.com\thuawei12#$\n'
v14 = test3.expandtabs(20)
print(v14)

test4 = '②'
v16 = test4.isdecimal()     # 主要用它
v17 = test4.isdigit()       # 俩个都是判断是否是数字,这个更厉害
v19 = test4.isnumeric()     # 是否是数字,支持中文数字
print(v16, v17, v19)

a = "_123"
a1 = "def"
v18 = a1.isidentifier()        # 字母,数字,下划线 : 标识符  def  class
print(v18)

test5 = 'sdjfoiasjdf\ndsaf'
v20 = test5.isprintable()       # 是否有不可见的字符,制表符,换行
print(v20)

test6 = ' '
v21 = test6.isspace()           # 判断是否全部为空格
print(v21)

test7 = 'tsdsa hsdfs jjsdfjsodf and is dog'
v23 = test7.title()             # 转化为标题
print("v23:", v23)
v22 = test7.istitle()           # 是否为标题
print("v22:",v22)

test8 = '你是风儿我是沙儿'
print(test8)
t = ' '
v24 = t.join(test8)             # 将字符串中的每一个元素按照指定分隔符进行拼接(重要)
print(v24)

test9 = 'asdsad'
v28 = test9.islower()           # 是否全部是小写
v29 = test9.lower()             # 变成小写
print(v28, v29)

v30 = test9.isupper()           # 是否全部是大写
v31 = test9.upper()             # 变成大写
print(v30, v31 )

test10 = '   cuihaicheng '
v32 = test10.lstrip()
v33 = test10.rstrip()
v34 = test10.strip()            # 处理空白,\t,\n,字符中的元素(“x”)
print(v32)
print(v33)
print(v34)

test11 = 'abcdefghijklmnopqrstuvwxyz'
m = str.maketrans('asdfghjkl', '123456789')         # 定义对应关系
new_v = test11.translate(m)                         # 按照对应关系进行替换
print(new_v)

test12 = 'test is a good action'
v35 = test12.partition('o')                         # 从前开始将字符串分割成三份
print(v35)
v36 = test12.rpartition('o')                        # 从后开始将字符串分割成三份
print(v36)
v37 = test12.split('o', 1)                          # 从前开始将字符串分割,不带分隔符,任意指定分成数
print(v37)
v38 = test12.rsplit('o', 2)                         # 从前开始将字符串分割,不带分隔符,任意指定分成数
print(v38)

test13 = 'sadasjd\nasdff\nsadd\nasfadfsdf'
v39 = test13.splitlines(true)                       # 分割,只能根据true,false:是否保留换行符
print(v39)

test14 = 'chdsow'
v40 = test14.startswith('a')                        # 是否以某个字符开始
v41 = test14.endswith('w')                          # 是否以某个字符结束
print(v40, v41)

test15 = "wqejijoomk"
v42 = test15.swapcase()                             # 大小写转换
print(v42)

test16 = 'cuicuicuicuicui'
v43 = test16.replace("ui", "aa", 3)                 # 替换字符,数字为前几个
print(v43)

"""
经常使用的有7个
join        将字符串中的每一个元素按照指定分隔符进行拼接
split       从前开始将字符串分割,不带分隔符,任意指定分成数
find        从开始往后找,找到第一个后,获取其位置    3,8 表示大于等于3小于8
strip       处理空白,\t,\n,字符中的元素(“x”)
upper       变成大写
lower       变成小写
replace     替换字符,数字为前几个
"""

# 字符串的灰魔法(其他数据类型都可以用)

str1 = "你是风儿我是沙儿,缠缠绵绵到天涯"
s1 = str1[2]                 # 1、索引,下标,获取字符串中的某个字符
s2 = str1[0:4]               # 2、切片,获取0=<,<4
s3 = len(str1)               # 3、len获取当前字符有几个字符组成,python 2中一个字符3个字节
print(s1, s2, s3)

count = 0
while count < len(str1):
    print(str1[count])
    count += 1
print("========end========")

for chc in str1:            # 4、for循环
    print(chc)

ra = range(0, 20, 5)        # 5、列出0-20,以5为步长
for it in ra:
    print(it)

# 将文件对应的索引打印出来

# test17 = input("输入内容>>>")
# print(test17)
# le = len(test17)
# print("len is :")
# r = range(0, le)
# for item in r:
#     print(item, "\t", test17[item])
test17 = input("输入内容>>>")
for item in range(0, len(test17)):
    print(item, "\t", test17[item])