当前位置: 移动技术网 > IT编程>脚本编程>Python > python学习-14 基本数据类型3

python学习-14 基本数据类型3

2019年06月20日  | 移动技术网IT编程  | 我要评论

1.字符串

获取字符串的字符,例如:

test = 'abcd'
a= test[0]              # 通过索引,下标,获取字符串中的某一个字符
print(a)

b = test[0:1]           # 通过下标的 范围获取字符串中的一些字符            #0 <= ** <1
print(b)

c =len(test)            # 获取字符串中有多少字符组成(不是下标)
print(c)

运算结果:

a
a
4

process finished with exit code 0

 2.list列表     用[ ]表示的  ,例如:a = [love,friend,home,"你好"]

test = [123,'adss']
print(type(test),test)

运算结果:

<class 'list'> [123, 'adss']

process finished with exit code 0

3.利用循环获取字符串中的每个字符

第一种:

test = '你好\t谢谢'
count = 0
while count < len(test):
    a=test[count]
    print(a)
    count += 1

运算结果:

你
好
    
谢
谢

process finished with exit code 0

第二种:

for 变量名 in 字符串:

代码块

print

      

test = '你好\t谢谢'
for a in test:
    print(a)

运算结果:

你
好
    
谢
谢

process finished with exit code 0

注意:字符串一旦创建无法修改,如果要修改,则会创建新的字符串。

4.替换

test = 'abcabcabc'
a = test.replace('a','q',1)         # 将q替换第一个a
print(a)

运算结果:

qbcabcabc

process finished with exit code 0

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网