阅读背景:

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

来源:互联网 
# "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\[email protected]\tHuawei12#$\ncuiahicheng\[email protected]\tHuawei12#$\ncuiahicheng\[email protected]\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])
# "adcbdefg"
# "a"

# 断定某个东西是不是在里面包括  in  | not i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: