栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python字符串相关操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python字符串相关操作

question1:运算优先级问题

1.乘除优先加减

2.位运算 == 算数运算 > 比较运算 > 赋值运算 > 逻辑运算

 

a,b,x,y = 10, 20, 30, 40

d = (5 + 10 * x)/5 -13 * (y - 1) * (a + b)/x + 9*(5 / x + (12 + x)/y)

print(d)

question2:input()的使用

从控制台输入用户的月薪,进行运算计算出年薪。打印输出用户的年薪

notes:input()获得的为string,要转为int

month_salary = input()

month_salary = int(month_salary)

year_salary = 12 * month_salary 

print(year_salary)

questions3:字符串的倒序输出

str = 'to be or not to be'
len_str = len(str)
str_reverse = str[::-1]
print(str_reverse)


questions4:输出指定位置字符

notes:第一个位置为0

str = 'sxtsxtsxtsxtsxt'

'''
place = 0 3 6 9 12
'''

str_s = str[0:len(str):3]

print(str_s)

question5:is 的含义和字符串驻留机制

a = "abd_33"
b = "adb_33"
c = "dd#"
d = "dd#"

e = a is b
f = c is d
print(e,f)

a is b ture

c is d false

a is b,a b满足python的字符串驻留机制,所以ture

c is d,c d不满足,false

驻留机制出发条件:字符串仅包含下划线(_)、字母和数字

question6:format使用


c = "名字是{name},年龄是{age}"

print(c)

print(c.format(age=19,name='高淇'))

'''

>>>
    名字是{name},年龄是{age}
    名字是高淇,年龄是19

'''
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339413.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号