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

python字符串切片

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

python字符串切片

           # type
a = 10     # number
b = '1'    # string
c = [1,2,3] # list



#  字符串切片
s   =   'python'
#index   012345
print(s[0])  # get value p from string    
print(s[3])  # h
print(s[::-1])   #transpose    nohtyp
print(s[0:2:1])   # py   0 1  

 string_name[start_value:end_value:steps]
# The start value indicates the start point
# steps indicates how many I add each time
# end_value indicates the end point ,but  the number  doesn't show
print(s[0:5:2])   # pto   0 2 4

练习

#  test         
d = 'nihaoyaqingti'
#    0123456789 10 11 12
print(d[1:11:3])   # ioqg    1 4 7 10

# test2
# there is a 3-digit number,and when you add 396,
# it reverses the original number .
# fin all of these 3-digit numer.
# such as    105+396=501

# create number  100~999      100   101    102   103   104.....999
for x in range(100,1000):
    s = x+396  # add 396,it reverses the original number .105+396
    s = str(s)  # change the type to string
    c = s[::-1] # reverse   501----->105
    c = int(c) # change type to number(integer)
    if x==c:
        print(x)
    
#homework 
1,please enter a number,judge if the number is a palindrome.
such as  1001   2002   1991  3113   symmetry

2,
f = 'abcdefgh'
print(f[1:6:2])   # result is ?
print(f[2:8:3])   # result is ?
print(f[0:3:1])   # result is ?
print(f[2:5:3])   # result is ?
'dfh'
'aceg'
'hgfedcba'


 

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

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

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