码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-06-19 20:19:50      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:for   基本数据类型   lov   exit   代码块   code   print   div   python学习   

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

 

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

标签:for   基本数据类型   lov   exit   代码块   code   print   div   python学习   

原文地址:https://www.cnblogs.com/liujinjing521/p/11053872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!