Crossin的编程教室

标题: 【Python 第30课】 字符串的索引和切片 [打印本页]

作者: crossin先生    时间: 2014-4-24 18:35
标题: 【Python 第30课】 字符串的索引和切片
前两天承蒙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


作者: cross    时间: 2015-4-23 10:56

作者: lovia    时间: 2015-5-28 00:01
  1. #coding:utf-8
  2. #遍历
  3. word='helloword'
  4. for c in word:
  5.     print c
  6. print
  7. #索引
  8. print word[0]
  9. print word[-1]
  10. print
  11. #切片
  12. print word[0:3]
复制代码

作者: catherinemic    时间: 2016-1-2 14:51
  1. word = 'helloworld'
  2. for c in word:
  3.     print c,
  4. print word[0],word[-2]
  5. print word[1:-2]
  6. newword=','.join(word)
  7. print newword
复制代码

Python15.png (6.07 KB, 下载次数: 360)

Python15.png


作者: catherinemic    时间: 2016-1-2 14:55
crossin老师那个点球的小游戏最后一点收尾在哪里呢?
作者: crossin先生    时间: 2016-1-2 23:50
catherinemic 发表于 2016-1-2 14:55
crossin老师那个点球的小游戏最后一点收尾在哪里呢?

http://mp.weixin.qq.com/mp/appms ... b44#wechat_redirect

这个吗?
作者: catherinemic    时间: 2016-1-3 10:53
crossin先生 发表于 2016-1-2 23:50
http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5MDEyMDk4Mw==&appmsgid=10000133&itemidx=1&sign=93c ...

是哒,谢谢crossin先生!
作者: catherinemic    时间: 2016-1-3 11:11
catherinemic 发表于 2016-1-3 10:53
是哒,谢谢crossin先生!

crossin先生,关于点球小游戏的设置提前结束那部分,在定义kick函数的时候,最后有一个return False,请问当提前结束的条件被触发时,按照if条件会返回True,之后又有一个return False,这样不会冲突吗?
作者: crossin先生    时间: 2016-1-3 12:04
catherinemic 发表于 2016-1-3 11:11
crossin先生,关于点球小游戏的设置提前结束那部分,在定义kick函数的时候,最后有一个return False,请 ...

执行到return,函数就返回结束了。不会再向下执行
作者: catherinemic    时间: 2016-1-3 15:26
crossin先生 发表于 2016-1-3 12:04
执行到return,函数就返回结束了。不会再向下执行

明白了,谢谢crossin老师!
作者: 德坤    时间: 2016-4-5 12:36
图片挂啦   Crossin老师
作者: crossin先生    时间: 2016-4-5 20:21
德坤 发表于 2016-4-5 12:36
图片挂啦   Crossin老师

已改
作者: rjq696    时间: 2016-8-28 20:01
2. 索引访问
通过[]加索引的方式,访问字符串中的某个字符。
print word[0]
print word[-2]

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

能不还是不能呀? crossin老师。
作者: crossin先生    时间: 2016-8-29 10:55
rjq696 发表于 2016-8-28 20:01
2. 索引访问
通过[]加索引的方式,访问字符串中的某个字符。
print word[0]

“不能”,已改
作者: 司南    时间: 2018-4-8 21:22
本节练习代码:
  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)
复制代码

作者: 司南    时间: 2018-4-8 22:24
本帖最后由 司南 于 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.         
复制代码

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

不算滥用吧,确实需要break
作者: 司南    时间: 2018-4-9 16:40
crossin先生 发表于 2018-4-8 22:58
不算滥用吧,确实需要break

好哒,多谢Crossin先生~
作者: yuyu1111    时间: 2018-5-20 16:33
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’   呢?

作者: crossin先生    时间: 2018-5-21 10:55
yuyu1111 发表于 2018-5-20 16:33
crossin先生,想请教一下

word='helloworld'

教程里没用print,是 shell 的输出,这两个效果是不一样的




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5