跟我一起学python(2)

时间:2023-03-08 19:54:26

学习总结:

1、数据类型

a、数据:表示一种状态

b、python不存在字符类型

c、可变与不可变

跟我一起学python(2)

d、x = 10  既 x = int(10)

跟我一起学python(2)

2、字符编码

3、文件处理

详细:

  数据类型:

is关键字  内存空间是否一样  x = 12  y=13   x is y  False

m=123 n=123  id(n) id(m) 一样  m is n  True  因为python对于数据量小的情况下 都占用同一块空间

字符串:

优先掌握的操作:

按索引取值:

name = "egon";
print(name[0],type(name[0]));
print(name[-2]);

      切片(顺头不顾尾,步长):

msg = "hello world";
print(msg[::-1]);

      长度(len)--- 数字没有长度,字符串有长度

lenTest = "你好?怎么说呢";
print(len(lenTest));

      成员运算  In   not in

msg1 = "hello yangtong";
print("llo " in msg1);

       移除空白 strip

 //23423  234234
password = " 23423 234234 "
print(password.strip());
//23423 234234
password = "*********23423 234234**************"
print(password.strip("*"));

     切分 split

user_info = "root:x:0:0:/root:/bin/bash"
print(user_info.split(":")[0]);
cmd = "put a.txt";
print(cmd.split())
filepath = "put /a/b/c/d/a.txt";
print(filepath.split(maxsplit = 1))

     次要掌握的操作:

     

msg = "      yangtong            ";
print(msg.lstrip())
print(msg.rstrip())

    什么开头   什么结尾

msg = "jiangziya_SB";
print(msg.startswith("jiangziya"));
print(msg.endswith("SB"))

    replace

msg = "haohao have a girl,haohao is good;"
print(msg.replace("haohao","tong",1));

   占位

print("%s %s" %('',123));
print("{} {}".format('',123))
print('{1}{0}'.format('',15))
print('{x},{y}'.format(y=13,x='hello'));

  find rfind与index rindex

msg = "hello world"
# 是否有子字符串 相当于indexOf
print(msg.find('ell'))
# 找不到会报错 其他的和find一样
print(msg.index(''))

  count

msg = 'hello world'
# 范围 顾头不顾尾
print(msg.count('l',0,4));

  join

user_info = "root:x:0:0:asdasd"
l = user_info.split(":");
print(l);
test = ':'.join(l);
# 拼接按制定符号连接到一起
print(test);

  center ljust rjust zerofill

user_info = "hello"
# ============hello=============
# =========================hello
# hello=========================
# 0000000000000000000000000hello
print(user_info.center(30,"="));
print(user_info.rjust(30,"="));
print(user_info.ljust(30,"="));
print(user_info.zfill(30));

  其他

msg = "sdlfkj\tsdsdfsd"
# 控制制表符有几个
# sdlfkj sdsdfsd
print(msg.expandtabs(10))
msg = "abc bcd ksk"
# Abc bcd ksk
# ABC BCD KSK
# abc bcd ksk
# Abc Bcd Ksk
# ABC BCD KSK
print(msg.capitalize())
print(msg.upper());
print(msg.lower())
print(msg.title())
print(msg.swapcase())