string-example.py

# -*- coding: utf-8 -*-

# @Date    : 2018-10-22
# @Author  : Peng Shiyu

# string 模块提供了一些用于处理字符串类型的函数

#  字符串方法

import string
from functools import partial

text = "Monty Python’s Flying Circus"

print("upper", "=>", text.upper())
print("lower", "=>", text.lower())
print("split", "=>", text.split())
print("join", "=>","+".join(text.split()))
print("replace", "=>", text.replace("Python","Perl"))
print("find", "=>", text.find("Python"), text.find("Perl"))
print("count", "=>", text.count("n"))

"""
upper => MONTY PYTHON’S FLYING CIRCUS
lower => monty python’s flying circus
split => ['Monty', 'Python’s', 'Flying', 'Circus']
join => Monty+Python’s+Flying+Circus
replace => Monty Perl’s Flying Circus
find => 6 -1
count => 3
"""

# 字符串转为数字

print(int("12"))  # 字符串转数字
print(int("0x12", base=16))  # 16进制转10进制
print(int("012", base=8))  # 8进制转10进制
print(int("110", base=2))  # 2进制转10进制
"""
12
18
10
6
"""

# 使用偏函数
bin_to_int = partial(int, base=2)

print(bin_to_int("110"))

print(bin(12))   # 10进制转2进制(str)
print(oct(12))   # 10进制转8进制(str)
print(hex(12))   # 10进制转16进制(str)

"""
0b1100
0o14
0xc
"""