str=encode([encoding="utf-8]"[,error="strict"])
默认是utf-8 如果想用简体中文也可以用gb2312
strict:遇到错误的时候就抛出异常
也可以是ignore(忽视) 还可以是replace
if __name__ == '__main__':
value='你好,我是代码浪人,浪里浪'
byte=value.encode('GBK')
print(value)
print(byte)
》》》
你好,我是代码浪人,浪里浪
b'\xc4\xe3\xba\xc3\xa3\xac\xce\xd2\xca\xc7\xb4\xfa\xc2\xeb\xc0\xcb\xc8\xcb\xa3\xac\xc0\xcb\xc0\xef\xc0\xcb'
str=decode([encoding="utf-8]"[,error="strict"])
默认是utf-8 如果想用简体中文也可以用gb2312
strict:遇到错误的时候就抛出异常
也可以是ignore(忽视) 还可以是replace
if __name__ == '__main__':
value='你好,我是代码浪人,浪里浪'
byte=value.encode('GBK')
print(value)
print(byte)
print(byte.decode('GBK'))
》》》
你好,我是代码浪人,浪里浪
b'\xc4\xe3\xba\xc3\xa3\xac\xce\xd2\xca\xc7\xb4\xfa\xc2\xeb\xc0\xcb\xc8\xcb\xa3\xac\xc0\xcb\xc0\xef\xc0\xcb'
你好,我是代码浪人,浪里浪
直接使用加号(同类型,不同类型会报错)
if __name__ == '__main__':
value1="你好"
value2=123
print(value1+value2)

汉字在GBK/GB2312编码占2个字节
在UTF-8.uncode占3个字节(或者4个字节)
计算长度提供len(string) 函数计算方法
if __name__ == '__main__':
value1="你好"
print(len(value1))
print(len(value1.encode('GBK')))
》》》
2
4
string[start🔚step]
start:开始位置,默认为0
end:结束位置,不包含
step:步长,默认为1
if __name__ == '__main__':
value1="你好345678"
print(value1[2:])
print(value1[:2])
print(value1[0:8:2])
》》》
345678
你好
你357
str.split(sep,maxsplit)
str:要分割的字符串
sep:指定分隔符,默认为None
maxsplit: 可选参数,默认为-1-> 分割次数
strnew=str.join(iterable)
strnew:新的字符串
string:用于合并时候的分隔符号
iterable:可迭代对象,将迭代对象中的所有元素按照分个字符串进行拼接
list_str=['我','爱','你']
list_new='*'.join(list_str)
print(list_new)
》》》
我*爱*你
str.count(sub[,start[,end]])
str检索的字符串
sub:需要检索的子字符串
start:开始位置
end:结束位置
str='123456'
print(str.count(6))
》》》
1
str.find(sub[,start[,end]])
str检索的字符串
sub:需要检索的子字符串
start:开始位置
end:结束位置
str='123456'
print(str.find(6))
》》》
5
if __name__ == '__main__':
list_str=['我','爱','你']
print('爱' in list_str)
》》》
True
str.index(sub[,start[,end]])
str检索的字符串
sub:需要检索的子字符串
start:开始位置
end:结束位置
str.startswitch(prefix[,start[,end]])
str检索的字符串
prefix:需要检索的子字符串
start:开始位置
end:结束位置
if __name__ == '__main__':
list_str=['我','爱','你']
print(list_str.startswitch('我'))
》》》
True
str.endswitch(prefix[,start[,end]])
str检索的字符串
prefix:需要检索的子字符串
start:开始位置
end:结束位置
if __name__ == '__main__':
list_str=['我','爱','你']
print(list_str.endswitch('你'))
》》》
True
if __name__ == '__main__':
str='ABC'
print(str.lower())
str='abc'
print(str.upper())
》》》
abc
ABC
str.strip([char])//默认为是去除首尾空字符和特殊字符串,char是可选,可指定多个
str.lstrip([char])//默认为是去除左边空字符和特殊字符串,char是可选,可指定多个,
str.rstrip([char])//默认为是去除右边空字符和特殊字符串,char是可选,可指定多个
Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。
print ("我叫 %s 今年 %d 岁!" % ('小明', 10))
我叫 小明 今年 10 岁!

格式化操作符辅助指令:

Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
'hello world'
>>> "{0} {1}".format("hello", "world") # 设置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
'world hello world'
也可以设置参数
# -*- coding: UTF-8 -*-
print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
# 通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site))
# 通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
网站名:菜鸟教程, 地址 www.runoob.com
网站名:菜鸟教程, 地址 www.runoob.com
网站名:菜鸟教程, 地址 www.runoob.com
也可以向 str.format() 传入对象:
class AssignValue(object):
def __init__(self, value):
self.value = value
my_value = AssignValue(6)
print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
数字
>>> print("{:.2f}".format(3.1415926))
3.14

此外我们可以使用大括号 {} 来转义大括号,如下实例:
print ("{} 对应的位置是 {{0}}".format("runoob"))
》》》
runoob 对应的位置是 {0}