python 定义字符串变量

编程入门 行业动态 更新时间:2024-10-05 11:18:14

python 定义<a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串变量"/>

python 定义字符串变量

1.定义变量及打印输出

a=1

#python输出方式

print(a)

#%s %d 限制输出的是字符串还是整数数字

name='M-x-M'

print('My name is %s' % name)

age=18

print('I`m %d years old '% age)

#普通打印

job='python'

print('I`m learning',job)

#多行打印

print('''--------day1------------

多行打印效果测试

My name is %s' ,I`m learning %s '''%(name,job))

2.字符串切片

str='开始学python'

print(str[0:1])

print(str[3:])

print(str[:-6])

3.字符串转译 加\

4.字符串编码转换

4.1转换为Unicode

str='字符串'

b_str=str.encode()

4.2转为字符串

str1=b_str.decode()

5.split()方法用于切割字符串

str1='hello world.txt'

cmd,filename=str1.split()

print(filename)

>>:

world.text

#--------------------------------

str1='hello|world.txt'

cmd,filename=str1.split('|')

print(filename)

>>:

world.text

6.strip()方法用于移除字符串前后的指定字符,默认为空格

str1=' hello world '

print(str1.strip())

>>:

hello world

#---------------------------

str1=str1='|hello|world|'

print(str1.strip('|'))

>>:

hello|world

更多推荐

python 定义字符串变量

本文发布于:2024-03-13 18:07:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   定义   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!