几个常用的函数
print 打印
print(‘*‘ * 10) => 打印出10个*
input 获取输入
name = input(‘What is your name?‘)
int float bool str 类型转换
num=int(‘12345‘)
len upper lower replace in
course=‘Python for Beginners‘ print(len(course)) =>20 print(course.upper())=>PYTHON FOR BEGINNERS
print(course.lower())=> python for beginners
print(course.title())=> Python For Beginners 首字母大写
print(course)=>Python for Beginners upper 并不会更改原有字符串
print(course.find(‘P‘)) => 0 找到第一个出现的索引 区分大小写
print(course.replace(‘Beginners‘,‘Absolute Beginners‘)) =>Python for Absolute Beginners 替换字符
print(‘Python‘ in course)=> True 是否包含某字符串
几个细节
字符串输入的几种方式
print(‘ ‘) print(" ") print(‘‘‘ 这种可换行输入 很方便 ‘‘‘)
字符串索引
coutse=‘my name is Jay Chou‘ print(coutse[0]) => m
print(coutse[-1])=> u
coutse=‘myname is Jay Chou‘print(coutse[0:3])=>myn 从0开始算数3个字符打印
print(coutse[0:])=>myname is Jay Chou 不指定结尾打印所有字符
print(coutse[:5])=>mynam 开头不指定默认从0开始
coutse=‘myname is Jay Chou‘another = coutse[:]print(another) =>myname is Jay Chou 用于clone字符串
格式化输入
first= ‘Jay‘ last= ‘Chou‘ msg = f‘{first}[{last}] is a coder‘ print(msg) =>Jay[Chou] is a coder
运算符
print(10/3) =>3.3333333333333335 print(10//3) =>3
print(10 % 3)=> 1
print((10**3)) =>1000 幂次运算
x = 10x +=3print(x) =>13
print(round(2.5)) => 2
print(round(2.51)) =>3
round(80.23456, 2) : 80.23 round(100.000056, 3) : 100.0 round(-100.000056, 3) : -100.0
print(abs(-2.5)) => 2.5
运算函数模块 math
import math
print(math.ceil(2.1)) 天花板函数 向上取整 => 3
print(math.floor(2.1)) 向下取整 =>2
原文地址:https://www.cnblogs.com/dcxy/p/12362477.html
时间: 2024-10-08 09:27:53