1.print的使用

1)在使用print进行打印时,在print最后,python解释器会自动的添加换行符,通过end控制

1 >>> print("你好",end="")
2 你好>>>
3 >>> print("你好",end="\n")
4 你好
5 >>> print("你好",end="世界")
6 你好世界>>>
View Code

2)在字符串之间若用逗号分割,输出时为空格

1 >>> print("你好","世界")
2 你好 世界
3 
4 >>> a = 10
5 >>> print("a= ",a)
6 a=  10
View Code

 2.输出(input)

1)通过input输入的东西数据类型都是字符串,即使输入的是数字,两个字符串可通过+拼接起来

1 name = input("请输入名字:")
2 alias = input("请输入称号:")
3 print(name + alias )
4 
5 输入输出:
6 请输入名字:九尾妖狐
7 请输入称号:阿狸
8 九尾妖狐阿狸
View Code

2)格式化输出

①通过%s占位符

 1 name = input("请输入名字:")
 2 age = input("请输入年龄:")
 3 skill = input("请输入技能:")
 4 sex = input("请输入性别:")
 5 print("英雄名:%s,年龄:%s,技能:%s,性别:%s" % (name,age,skill,sex))
 6 
 7 输出输入:
 8 请输入名字:九尾妖狐
 9 请输入年龄:18
10 请输入技能:灵魂突袭
11 请输入性别:女
12 英雄名:九尾妖狐,年龄:18,技能:灵魂突袭,性别:女
View Code

3.逻辑运算符

1)and or not 同时存在,先算括号,然后算not,然后算and,最后算or

2) x or y 如果x==0,那么就是y,否则就是x

 1 print(1 or 2)
 2 print(2 or 3)
 3 print(0 or 3)
 4 print(0 or 4)
 5 
 6 输出:
 7 1
 8 2
 9 3
10 4
View Code

3)and 和 or 相反,x and y 如果x==0,那么就是x,否则就是y

注:若and和or同时存在,则先算and在算or

4.int类型

1)计算整数转换为二进制的长度

1 num = 3
2 print(num.bit_length())
3 输出:
4 2
View Code

5.字符串

1)字符串的拼接:字符串之间可以使用+将字符串进行拼接

1 >>> s1 = "LOL"
2 >>> s2 = "DNF"
3 >>> s3 =s1+s2
4 >>> print(s3)
5 输出:
6 LOLDNF
View Code

2)字符串的乘法(即将字符串重复多少次)

1 >>> s1 = "你好"
2 >>> print(s1*3)
3 输出:
4 你好你好你好
View Code

 3)如果字符串中有了占位符,那么后面的所有%都是占位,需要转义(转义时使用两个%号)

1 name = "阿狸"
2 print("%s喜欢%%2的水果" % name)
3 
4 输出:
5 阿狸喜欢%2的水果
View Code

 4)字符串转为整数类型

1 num = "3"
2 print(type(num))
3 n = int(num)
4 print(type(n))
5 输出:
6 <class 'str'>
7 <class 'int'>
View Code

5)整数类型转为字符串类型

1 num = 3
2 print(type(num))
3 n = str(num)
4 print(type(n))
5 输出:
6 <class 'int'>
7 <class 'str'>
View Code

 6)bool类型转换为数字

 View Code

 注:0为False 非0为True,1的效率比True要高

总结:想转换成什么数据类型,就用什么类型把目标括起来

 空字符串表示False 非空字符串表示True;空格也是非空

1 if "你好":
2     print("哈哈")
3 else:
4     print("呵呵")
5 输出:
6 哈哈
View Code

 空字符串表示False

1 if "":
2     print("哈哈")
3 else:
4     print("呵呵")
5 输出:
6 呵呵
View Code

 对于None,为真空,表示False

1 m = None
2 if m:
3     print("哈哈")
4 else:
5     print("呵呵")
6 输出:
7 呵呵
View Code

 总结:空的东西都是false,非空的都是True

6.字符串切片(切片可以对字符串进行截取)

1)通过索引获取到的字符串内容,还是一个字符串

2)语法:

         s[起使位置:结束位置]       切片时顾头不顾尾

3)字符串切片+字符串拼接

1 s = "我爱北京天安门"
2 s2 = s[:2] + s[4:]                   #不写,默认从0开始
3 print(s2)
4 输出:
5 我爱天安门
View Code

 

 4)什么都不写,默认从头开始到结尾

1 s = "我爱北京天安门"
2 s2 = s[:]
3 print(s2)
4 输出:
5 我爱北京天安门
View Code

  5)从-2切到结尾

1 s = "我爱北京天安门"
2 s2 = s[-3:]
3 print(s2)
4 输出:
5 天安门
View Code

6)步长

  语法: s[起使位置:结束位置:步长]

1 s = "我是梅西,我很慌"
2 s1 = s[1:5:2]
3 print(s1)
4 输出:
5 是西
View Code

 

1 s = "我是梅西,我很慌"
2 s1 = s[1::3]
3 print(s1)
4 输出:
5 是,慌
View Code

7)反着取(步长为负值,代表反着取)

1 s = "我是梅西,我很慌"
2 s1 = s[6:2:-2]               #从右往左取,没隔两个取一个
3 print(s1)
4 输出:
5 很,
View Code

 8)从后往前取值

1 s = "我是梅西,我很慌"
2 s1 = s[-1:-6:-2]
3 print(s1)
View Code

 7.字符串的常用方法

  注:字符串是不可变对象,所以任何操作对元字符串都不会有任何影响

1)将字符串首字母大写

1 s = "lol dnf cp qq飞车"
2 s1 = s.capitalize()
3 print(s)              #源字符串不发生改变
4 print(s1)
5 输出:
6 lol dnf cp qq飞车
7 Lol dnf cp qq飞车
View Code

2)将字符串中所有字母转换为小写

1 s = "DNF AND LOL and LPL"
2 s1 = s.lower()
3 print(s)              #源字符串不发生改变
4 print(s1)
5 输出:
6 DNF AND LOL and LPL
7 dnf and lol and lpl
View Code

3)将字符串中所有字母转换为大写

1 s = "lol AND dnf and lpl"
2 s1 = s.upper()
3 print(s)              #源字符串不发生改变
4 print(s1)
5 输出:
6 lol AND dnf and lpl
7 LOL AND DNF AND LPL
View Code

 例:输入大小写都退出(在程序需要判断不区分大小写的时候,可用到)

1 while True:
2     content = input("请输入:")
3     if content.upper() == 'Q':
4         break
5     print("你输入了:",content)
View Code

4)大小写转换

1 s = "dnf AND LOL AND lpl"
2 s1 = s.swapcase()
3 print(s1)
4 输出:
5 DNF and lol and LPL
View Code

5)被特殊字符隔开的,首字母都大写(包括被汉字隔开)

1 s = "lol and哈哈dnf AND lpl"
2 s1 = s.title()
3 print(s1)
4 输出:
5 Lol And哈哈Dnf And Lpl
View Code

 6)将字符串拉长为固定长度,并居中;其余位置用其他字符补齐(默认为空格)

1 s = "哈哈哈"
2 print(s.center(9,"*"))
3 输出
4 ***哈哈哈***
View Code

7)去掉字符串两侧的空格(strip)

 1 username = input("请输入用户名:").strip()
 2 password = input("请输入密码:").strip()
 3 if username == "zdz" and password == "123":
 4     print("登录正确")
 5 else:
 6     print("登录错误")
 7 输出:
 8 请输入用户名:   zdz 
 9 请输入密码:   123
10 登录正确
View Code

8)去掉字符串左右两边的指定字符串(字符串中间的无法去掉)

1 s = "abc哈哈呵呵嘿嘿呼呼bc"
2 print(s.strip("abc"))
3 输出:
4 哈哈呵呵嘿嘿呼呼
View Code

9)字符串替换(replace )

1 s = "lol and cf and lpl"
2 print(s.replace(" ",""))      #把空格替换为空字符串
3 输出:
4 lolandcfandlpl
View Code

 #只替换两个

1 s = "lol and lol and lpl and lol"
2 print(s.replace("lol","cf",2))
3 输出:
4 cf and cf and lpl and lol
View Code

 10)字符串切割(split)----切割完成的数据类型为列表,列表中装的是字符串

1 s = "lol_dnf_cf_lpl+ppp"
2 print(s.split("_"))
3 输出:
4 ['lol', 'dnf', 'cf', 'lpl+ppp']
View Code

注:如果字符是贴着字符串边切割的,那么是切出来的列表中是空字符(即切边时会产生空字符串)

1 s = "_lol_dnf_cf_lpl+ppp_"
2 print(s.split("_"))
3 输出:
4 ['', 'lol', 'dnf', 'cf', 'lpl+ppp', '']
View Code

 11)字符串指定位置进行格式化输出(如果不指定位置,默认从前往后)

1 s = "我是{2},今年{1}岁了,喜欢吃{0}".format("苹果",18,"zz")
2 print(s)
3 输出:
4 我是zz,今年18岁了,喜欢吃苹果
View Code

12)字符串指定变量名进行格式化输出

1 s = "我是{name},今年{age}岁了,喜欢吃{fruits}".format(name = "zz",age = 18,fruits = "apple")
2 print(s)
3 输出:
4 我是zz,今年18岁了,喜欢吃apple
View Code

13)查找:判断字符串是不是以某个字符开头

1 s = "我爱北京天安门"
2 print(s.startswith(""))
3 输出:
4 True
View Code

14)查找:判断自符出是不是以某个字符结尾

1 s = "我爱北京天安门"
2 print(s.endswith("天安门"))
3 输出:
4 True
View Code

15)查找:计算某个字符在字符串中出现的次数

1 s = "我爱北京天安门,我爱中国"
2 print(s.count("我爱"))
3 输出:
4 2
View Code

 16)查找:某个字符在原字符串中的位置(如果没出现,返回-1)

1 s = "我爱北京天安门,我爱中国"
2 print(s.find("天安门"))
3 输出
4 4
View Code

 17)查找:通过切片方式查找某个字符在字符串中的位置

1 s = "我爱北京天安门,我爱中国"
2 print(s.find("我爱",3))
3 输出:
4 8
View Code

 18)查找:查找某个字符在原字符串中的索引位置(如果没有该字符。直接报错)

1 s = "我爱北京天安门,我爱中国"
2 print(s.index("我爱"))
3 输出:
4 0
View Code

注:find和index的区别:find如果没查到字符返回-1,index如果没查到内容直接报错

19)判断:判断字符串是否都为数字

1 s = "123"
2 print(s.isdigit())
3 输出:
4 True
View Code

 20)判断:判断字符串是否都由字母组成

1 s = "abd"
2 print(s.isalpha())
3 输出:
4 True
View Code

21) 判断:判断字符串是否都由字母或数字组成组成

1 s = "abd1"
2 print(s.isalnum())
3 输出:
4 True
View Code

 22)判断:判断字符是否为数字,包括中文数字,大写数字

 1 s1 = "123"
 2 s2 = "一二三四五"
 3 s3 = "叁佰伍拾肆"
 4 print(s1.isnumeric())
 5 print(s2.isnumeric())
 6 print(s3.isnumeric())
 7 输出:
 8 True
 9 True
10 True
View Code

 例:判断一个数是否为小数

 1 s = "-123.123"
 2 s = s.replace("-","")
 3 if s.isdigit():
 4     print("是整数")
 5 else:
 6     if s.count(".") == 1 and not s.startswith(".") and not s.endswith("."):
 7         print("是小数")
 8     else:
 9         print("不是小数")
10 输出:
11 是小数
View Code

24)计算:求字符串的长度

 1 s = "我爱北京天安门"
 2 i = len(s)
 3 print(i)
 4 print("==============")
 5 i = s.__len__()
 6 print(i)
 7 输出:
 8 7
 9 ==============
10 7
View Code

注:len()函数为内置函数,在该函数内部,实际调用的是__len__()

例:把字符串从头到尾遍历

①while循环遍历:有索引

1 s = "我爱北京天安门"
2 count = 0
3 while count <= len(s)-1:
4     print(s[count])
5     count =count +1
View Code

注:索引是从0开始,长度是从1开始

②for循环遍历:简单但是没有索引

1 #将字符串s中的每一个字符交给前面的变量c循环
2 s = "我爱北京天安门"
3 for c in s:
4     print(c)
View Code

补充:for循环语法

for 变量 in 可迭代对象(可一个一个往外拿即为可迭代对象)
    循环体