ch_str = 'Python数据分析升级版'
print(ch_str)
type(ch_str)
Python数据分析升级版
str
# str转换成bytes,用encode
b_str = ch_str.encode('utf-8')
print(b_str)
type(b_str)
b'Python\xe6\x95\xb0\xe6\x8d\xae\xe5\x88\x86\xe6\x9e\x90\xe5\x8d\x87\xe7\xba\xa7\xe7\x89\x88'
bytes
# bytes转换成str,用decode
ch_str2 = b_str.decode('utf-8')
print(ch_str2)
type(ch_str2)
Python数据分析升级版
str
'{0}, {1}'.format('Python', 2017)
'Python, 2017'
'{}, {}'.format('Python', 2017)
'Python, 2017'
'{0}, {1}, {0}'.format('Python', 2017)
'Python, 2017, Python'
'{name}, {year}'.format(year=2017, name='Python')
'Python, 2017'
dict_val = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print(dict_val.keys())
print(type(dict_val.keys()))
dict_keys(['Name', 'Age', 'Class'])
<class 'dict_keys'>
for key in dict_val.keys():
print('{}--{}'.format(key, type(key)))
Name--<class 'str'>
Age--<class 'str'>
Class--<class 'str'>
print(dict_val.values())
print(type(dict_val.values()))
dict_values(['Zara', 7, 'First'])
<class 'dict_values'>
for value in dict_val.values():
print('{}--{}'.format(value, type(value)))
Zara--<class 'str'>
7--<class 'int'>
First--<class 'str'>
print(dict_val.items())
print(type(dict_val.items()))
dict_items([('Name', 'Zara'), ('Age', 7), ('Class', 'First')])
<class 'dict_items'>
for item in dict_val.items():
print('{}--{}'.format(item, type(item)))
('Name', 'Zara')--<class 'tuple'>
('Age', 7)--<class 'tuple'>
('Class', 'First')--<class 'tuple'>