python基础----基础数据类型(不可变)--字符串

一、变量的命名规则

# 1.以英文数字下划线组成,不能以数字开头

# 2.变量名不可以和python中的关键字重名

# 3.变量名如果有多个单词,单词之间以下划线隔开

# month_of_year

# 4.变量名要做到见名知意

分别取个十百位:

guess_num = 435

x = guess_num // 100

y = guess_num // 10 % 10

# y = guess_num % 100 // 10

z = guess_num % 10

print(x, y, z)

二、字符串转义:

# \n 换行符

# \t 制表符

# \r 回车键

str06 = "这里\n\t是 '单引号' 这里\r是 \"双引号 \" ''' \"\"\""

# print(str06)

三、 字符串格式化

# %s 字符串

# %d 整型

# %f 浮点型

# print(“我的名字是%s” %(变量))

#print((“我的名字是{}” .format(变量)))

四、 字符串的其它操作

# 1. 字符串相加

print("hello " + "world")

# 2. 字符串和整型相乘

print("hello" * 3)

# 3. 判断字符串是否相等\不相等

print('a' == 'a')

print('a' != 'a')

# 4. 返回指定字符串的长度 len()

str07 = "hello world"

print(len(str07)) # 11

五、字符串的一些方法

# 1. str.upper() 字符串全部大写

# print("hello worLD".upper())

# 2. str.lower() 字符串全部转小写

# print("hello World".lower())

# 3. str.capitalize() 把字符串的开头转大写

# print("hello".capitalize())

# 4. str.isupper() 判断字符串是否全部大写

# print("HEL".isupper())

# print("HeL".isupper())

# 5. str.islower() 判断字符串是否全部小写

# print("HEL".islower())

# print("hel".islower())

# 6. str.startswith() 判断字符串是否以指定子串开头

# print("hello world".startswith("hel"))

# print("hello world".startswith("hello "))

# print("hello world".startswith("el"))

# 7. str.endswith() 判断字符串是否以指定子串结尾

# print("hello world".endswith("rld"))

# 8. str.index() 返回指定子串在字符串中的位置

# print("hello world".index("wo"))

# print("hello world".index("l", 5))

# print("hello world".index("x")) # 找不到则报错

# 9. str.count() 返回指定子串在字符串中出现的次数

# print("hello wolrd".count(""))

# print("hello wolrd".count("l", 0, 6))

# 10. str.find() 返回指定子串在字符串中的位置,找不到则返回-1

# print("hello world".find("l"))

# print("hello world".find("x"))

# 11. str.rfind() 同上,从右边开始查找

# print("hello world".rfind("l"))

# 12. str.strip() 默认去除字符串两边的空格符、制表符、换行符

# print("\n \thello\t")

# print("\n \thello\t ".strip())

# print("## hello$##".strip("#$ "))

# str.lstrip() left strip 同上,只清除左边

# str.rstrip() right strip 同上,只清除右边

# 13. str.split() 根据指定的子串切割字符串,切割后是一个列表

# print("hello world".split()) # 默认根据空格切割

# print("https://www.baidu.com".split("."))

# print("https://www.baidu.com".split("/"))

# 14. str.replace() 替换字符串

# print("hello java java java".replace("java", "python"))

# print("hello java java java".replace("java", "python", 2))

# 15. str.encode() 把字符串编码为字节

# print("你好".encode())

# print("你好".encode("GBK"))

# 16. bytes.decode() 把字节解码成字符

# print(b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode())

# print(b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode("GBK"))

# 17. str.isdigit() 判断字符串是否全部是数字

# print("123".isdigit())

# print("123a".isdigit())

# 18. str.isalpha() 判断字符串是否全部是英文字母

# print("abc".encode().isalpha())

# print("abc中".encode().isalpha())

# 19. str.isalnum() 判断字符串是否全部是英文数字组成

# print("adb123".encode().isalnum())

# 20. str.isascii() 判断字符串是否全部ascii码表中的字符

# print("abc123.!@#$%^&*".isascii())

# print("abc123.!@#$%^&*中文".isascii())

# 21. str.join() 以字符串为分隔符,把可迭代对象转字符串

print("?".join(["h", "e", "l", "l", "o"]))

print(" ".join("hello"))

# 22. str.format() 字符串格式化

str01 = "%s like %s, %s like %s."

print(str01 % ("zs", "python", "ls", "java"))

str02 = "{} like {}, {} like {}."

print(str02.format("zs", "python", "zs", "java"))

str03 = "{0} like {1}, {0} like {2}."

print(str03.format("zs", "python", "java"))

str04 = "{n} like {h1}, {n} like {h2}."

print(str04.format(n="zs", h1="python", h2="java"))

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值