Python超入门(2)__迅速上手操作掌握Python

​​​​​​​ 

 # 5.字符串
# 5.字符串
course = "Python's Course for Beginner"  # ""内可使用'
#         012345678……
course2 = 'Python For "Beginner"'  # '' 内可使用"
course3 = ''' 
Hi John,
Here is our first email to you.
'''  # 多行字符串打印

print(course[0])  # P
print(course[-1])  # r
print(len(course))  # 28
print(course[7:11])  # s Co 从第7位到第10位
print(course[:6])  # Python 从第0位到第5位,不包括第六位
print(course[:])  # Python's Course for Beginner 从0到结尾
print(course[-1:2])  # 输出为空
# 6.格式化字符串
first_name = 'Luo'
last_name = 'Bing'
full_name = first_name + ' [' + last_name + '] is a coder.'
full_name2 = f'{first_name} [{last_name}] is a coder.'  # 使用{}在字符串中动态插入值,需要使用f固定
print(full_name)  # Luo [Bing] is a coder.
print(full_name2)  # Luo [Bing] is a coder.
# 7.字符串方法
# 7.字符串方法
your_name = 'Your Name is Lilin'
print(len(your_name))  # 18 字符串长度
print(your_name.upper())  # YOUR NAME IS LILIN 大写
print(your_name.lower())  # your name is lilin 小写
print(your_name)  # Your Name is Lilin 原型
print(your_name.find('i'))  # 10 查找第一个字母为i的下标
print(your_name.find('b'))  # -1 未查找到第一个字母为b的下标
print(your_name.replace('i', 'a'))  # Your Name as Lalan 将所有 i 替换为 a
print(your_name.replace('Your', 'My'))  # My Name is Lilin
print('Python' in your_name)  # False; 'Python'在your_name中,否
print('Name' in your_name)  # True; 'Name'在your_name中,是
# 8.算术运算符 +、-、*、\、**、%
print(11 / 3)  # 3.6666666666666665 除
print(11 // 3)  # 3 整除:直接取整,去掉小数点后的数
print(2 ** 3)  # 8  立方,2*2*2
print(10 % 4)  # 取余 2
# 9.运算符优先级 (和其他编程语言一致)
x = (2 + 3) * 10 - 3 + 2 ** (2 + 1)
print(x)  # 5*10-3+2**3 = 60-3+8 = 55
# 10.数学函数
import math  # 导包,数学模块

x = -2.9
print(round(x))  # -3; round方法:四舍五入
print(abs(x)) # 2.9;取绝对值

print(math.ceil(x))  # -2; 数学模块:ceil方法 往上取整
print(math.floor(x))  # -3;数学模块:floor方法 往上取整

猜你喜欢

转载自blog.csdn.net/qq_57233919/article/details/132744605
今日推荐