shell> vi 2.py
- #!/usr/bin/env python
- str = ("i am a worker, and you are a student !")
- print str
- #split the str,if not specified the separator ,the whitespace is a separator,items is a sequence
- items = str.split()
- print items
- #join the str
- sep=":"
- items=sep.join(items)
- print items
执行这个脚本2.py,得到的结果为:
i am a worker, and you are a student !
['i', 'am', 'a', 'worker,', 'and', 'you', 'are', 'a', 'student', '!']
i:am:a:worker,:and:you:are:a:student:!
本文通过一个简单的Python脚本展示了如何进行字符串的操作,包括打印原始字符串、使用split方法按空白符分割字符串为列表,以及使用join方法将列表项用特定字符连接起来形成新的字符串。

7万+

被折叠的 条评论
为什么被折叠?



