设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 24747|回复: 19
打印 上一主题 下一主题

【Python 第30课】 字符串的索引和切片

[复制链接]

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
楼主
发表于 2014-4-24 18:35:16 |只看该作者 |正序浏览
前两天承蒙MacTalk池建强老师的推荐,让我们的学习队伍进一步壮大了。很多同学用的是Mac系统,而我是Linux党,很多Mac上的问题我没法帮忙解决。建议关注池老师的微信号,他经常会介绍一些Mac的使用技巧,让你更好地发挥Mac的强大功能。不用Mac的同学也建议去看看,他的文章有关技术和人文,相信你会得到不少启发。

微信号:Sagacity-Mac,直接搜索mactalk也可以看到。

#==== 关于字符串的事 ====#

之前说了,字符串和list有很多不得不说的事。今天就来说说字符串的一些与list相似的操作。

1. 遍历
通过for...in可以遍历字符串中的每一个字符。
word = 'helloworld'
for c in word:
   print c

2. 索引访问
通过[]加索引的方式,访问字符串中的某个字符。
print word[0]
print word[-2]

与list不同的是,字符串不能通过索引访问去更改其中的字符。
word[1] = 'a'
这样的赋值是错误的。

3. 切片
通过两个参数,截取一段子串,具体规则和list相同。
print word[5:7]
print word[:-5]
print word[:]

4. 连接字符
join方法也可以对字符串使用,作用就是用连接符把字符串中的每个字符重新连接成一个新字符串。
newword = ','.join(word)

slice.jpg

#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

20#
发表于 2018-5-21 10:55:27 |只看该作者
yuyu1111 发表于 2018-5-20 16:33
crossin先生,想请教一下

word='helloworld'

教程里没用print,是 shell 的输出,这两个效果是不一样的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

50

积分

注册会员

Rank: 2

19#
发表于 2018-5-20 16:33:06 |只看该作者
crossin先生,想请教一下

word='helloworld'
newword=','.join(word)
print(newword)

result==h,e,l,l,o,w,o,r,l,d
为什么不是课程里的‘h,e,l,l,o,w,o,r,l,d’   呢?
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

18#
发表于 2018-4-9 16:40:42 |只看该作者
crossin先生 发表于 2018-4-8 22:58
不算滥用吧,确实需要break

好哒,多谢Crossin先生~
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

17#
发表于 2018-4-8 22:58:54 |只看该作者
司南 发表于 2018-4-8 22:24
终于整出来了,总结经验:提前画好流程,整理思路,事半功倍
有个问题想请问一下Crossin先生,提前结束的部 ...

不算滥用吧,确实需要break
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

16#
发表于 2018-4-8 22:24:16 |只看该作者
本帖最后由 司南 于 2018-4-8 22:32 编辑

终于整出来了,总结经验:提前画好流程,整理思路,事半功倍
有个问题想请问一下Crossin先生,提前结束的部分,感觉自己滥用了break,请问有其他的解决办法啥的麽?
  1. #football_game.py
  2. #司南 2018.04.07
  3. #方向设定
  4. direction=['left','middle','right']
  5. #扑救方
  6. def keeper(name='computer'):
  7.     import random
  8.     dic=random.choice(direction)
  9.     if name=='computer':
  10.         print('天哪,快看,守门员扑向了%s方向' %dic)
  11.     else:
  12.         print('天哪,快看,守门员%s扑向了%s方向' %(name,dic))
  13.     return dic
  14. #射门方
  15. def shoot(man):
  16.     dic=input('请输入射球方向:left/middle/right')
  17.     print('快看,%s能否射进呢?' %man)
  18.     return dic
  19. #比赛规则
  20. def fight(keeperdic,shootdic):
  21.     if keeperdic==shootdic:
  22.         print('omg,the goalkeeper win!')
  23.         return True
  24.     else:
  25.         print('天哪,球进了!!')
  26.         return False
  27. #开始比赛(提前比赛)
  28. score_keeper=0
  29. score_shoot=0
  30. man=input('Hei,man please tell us your name:')
  31. round_game=eval(input('how many round do you do'))
  32. #判断总分胜负:
  33. def final():
  34.     if score_keeper>score_shoot:
  35.         print('总冠军:伟大的守门员!!')
  36.     elif score_keeper<score_shoot:
  37.         print('总冠军:%s 你赢啦!'%man)
  38.     else:
  39.         print('平手!决胜局来啦')
  40.         shootdic=shoot(man)
  41.         keeperdic=keeper()
  42.         res=fight(keeperdic,shootdic)
  43.         if res:
  44.             print('Everybody!!Winner is goalkeeper')
  45.         else:
  46.             print('Everybody!!Winner is %s'%man)
  47.    
  48. #开始比赛
  49. for i in range(round_game):
  50.         print('====Everybody the %dth round begin!===='%(i+1))
  51.         shootdic=shoot(man)
  52.         keeperdic=keeper()
  53.         res=fight(keeperdic,shootdic)
  54.         if res:
  55.             score_keeper=score_keeper+1
  56.             if score_keeper>=round_game//2+1:
  57.                 final()
  58.                 break
  59.         else:
  60.             score_shoot=score_shoot+1
  61.             if score_shoot>=round_game//2+1:
  62.                 final()
  63.                 break
  64.                


  65.    
  66.         
  67.    


  68.               

  69.    
  70.         
复制代码
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

15#
发表于 2018-4-8 21:22:20 |只看该作者
本节练习代码:
  1. word='hello world!'
  2. for i in word:
  3.     print(i)

  4. print(word[0])
  5. print(word[1:4])

  6. new_word=','.join(word)
  7. print(new_word)
  8. new_word2='.'.join(word)
  9. print(new_word2)
  10. new_word3=''.join(word)
  11. print(new_word3)

  12. new_word1=word.split()
  13. print(new_word1)
  14. x=['a','b','c']
  15. x1=','.join(x)
  16. print(x1)

  17. x2=['ab','cd','ex']
  18. x3=''.join(x2)
  19. print(x3)
复制代码
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

14#
发表于 2016-8-29 10:55:07 |只看该作者
rjq696 发表于 2016-8-28 20:01
2. 索引访问
通过[]加索引的方式,访问字符串中的某个字符。
print word[0]

“不能”,已改
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

28

积分

新手上路

Rank: 1

13#
发表于 2016-8-28 20:01:53 |只看该作者
2. 索引访问
通过[]加索引的方式,访问字符串中的某个字符。
print word[0]
print word[-2]

与list不同的是,字符串能不通过索引访问去更改其中的字符。
word[1] = 'a'
这样的赋值是错误的。

能不还是不能呀? crossin老师。
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

12#
发表于 2016-4-5 20:21:55 |只看该作者
德坤 发表于 2016-4-5 12:36
图片挂啦   Crossin老师

已改
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-2 12:39 , Processed in 0.033151 second(s), 29 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部