python (3)简单语法:字符串(strip函数),数据类型
一:字符串重复,索引,切片(字符串命令strip)
函数原型strip
声明:s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
注意:
1. 当rm为空时,默认删除空白符(包括'\n', '\r', '\t', ' ')
例如:
>>> a = ' 123 '
>>> a.strip() #删除前后
''
>>> a
' 123 '
>>> a.lstrip()#删除前面
'123 '
>>> a.rstrip()#删除后面
''
>>>
KeyboardInterrupt
>>> b = '\t\tabc'
>>> b
'\t\tabc'
>>> b.strip()
'abc'
>>> c = 'sdff\r\n'
>>> c
'sdff\r\n'
>>> c.strip()
'sdff'
>>>
2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。
例如 :
>>> a = '123abc'
>>> a.strip('')
'3abc' 结果是一样的
>>> a.strip('')
'3abc'
3.Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。
这三个函数都可传入一个参数,指定要去除的首尾字符。
需要注意的是,传入的是一个字符数组,编译器去除两端所有相应的字符,直到没有匹配的字符,比如:
来自:http://blog.csdn.net/crazyhacking/article/details/17223351
>>> theString = 'saaaay yes no yaaaass'
>>> print theString.strip('say')
yes no
>>>
theString依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。所以,输出的结果为:
yes no
比较简单吧,lstrip和rstrip原理是一样的。
注意:当没有传入参数时,是默认去除首尾空格的。
>>> theString = 'saaaay yes no yaaaass'
>>> print theString.strip('say')
yes no
>>> print theString.strip('say ') #say后面有空格
es no
>>> print theString.lstrip('say')
yes no yaaaass
>>> print theString.rstrip('say')
saaaay yes no
>>>
二:列表,元组,集合,字典
join的使用:
>>> a = ['','dd','aa']
>>> a
['', 'dd', 'aa']
>>> b = ';'.join(a)
>>> b
'11;dd;aa'
>>> print b
11;dd;aa
>>>
KeyboardInterrupt
>>> type(b)
<type 'str'>
>>>
三:python list中append()与extend()用法添加元素,remove,del删除元素
列表是以类的形式实现的。“创建”列表实际上是将一个类实例化。因此,列表有多种方法可以操作。
2. append() 方法向列表的尾部添加一个新的元素。只接受一个参数。
3. extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
append()用法示例:
>>> mylist=[1,2,3,'abc']
>>> mylist
[1, 2, 3, 'abc']
>>> mylist.append(4)
>>> mylist
[1, 2, 3, 'abc', 4]
>>> mylist.append('hello')
>>> mylist
[1, 2, 3, 'abc', 4, 'hello']
extend()用法示例:
>>> mylist
[1, 2, 3, 'abc', 4, 'hello', 'word']
>>> mylist.extend(['zizi'])
>>> mylist
[1, 2, 3, 'abc', 4, 'hello', 'word', 'zizi']
>>> mylist.extend(['qwse',1234])
>>> mylist
[1, 2, 3, 'abc', 4, 'hello', 'word', 'zizi', 'qwse', 1234]
>>> mylist.extend([123,566])
>>> mylist
[1, 2, 3, 'abc', 4, 'hello', 'word', 'zizi', 'qwse', 1234, 123, 566]
#错误例子
>>> mylist.expend([12w])
File "<stdin>", line 1
mylist.expend([12w])
^
SyntaxError: invalid synta
remove,pop,del
>>> mylist
[1, 2, 3, 'abc', 4, 'hello', 'word', 'zizi', 'qwse', 1234, 123, 566]
#del删除具体位置上的数字,列表从0开始
>>> del mylist[2]
>>> mylist
[1, 2, 'abc', 4, 'hello', 'word', 'zizi', 'qwse', 1234, 123, 566]
#pop()删除最后列表中的最后一个元素,并显示出来
>>> mylist.pop()
566
>>> mylist
[1, 2, 'abc', 4, 'hello', 'word', 'zizi', 'qwse', 1234, 123]
#remove 删除具体的元素
>>> mylist.remove('hello')
>>> mylist
[1, 2, 'abc', 4, 'word', 'zizi', 'qwse', 1234, 123]
#列表里允许有重复的元素出现
>>> mylist.append(123)
>>> mylist
[1, 2, 'abc', 4, 'word', 'zizi', 'qwse', 1234, 123, 123]
#列表中如果有相同的元素,则用remove 删除不成功
>>> mylist.rmove(123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'rmove'
>>> mylist
[1, 2, 'abc', 4, 'word', 'zizi', 'qwse', 1234, 123, 123]
#这个也是错误的
>>> mylist.remove(['abc',2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>>
python (3)简单语法:字符串(strip函数),数据类型的更多相关文章
- Python内置的字符串处理函数整理
Python内置的字符串处理函数整理 作者: 字体:[增加 减小] 类型:转载 时间:2013-01-29我要评论 Python内置的字符串处理函数整理,收集常用的Python 内置的各种字符串处理 ...
- SQL点滴3—一个简单的字符串分割函数
原文:SQL点滴3-一个简单的字符串分割函数 偶然在电脑里看到以前保存的这个函数,是将一个单独字符串切分成一组字符串,这里分隔符是英文逗号“,” 遇到其他情况只要稍加修改就好了 CREATE FUN ...
- 飘逸的python - 增强的格式化字符串format函数
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...
- (转)飘逸的python - 增强的格式化字符串format函数
原文:https://blog.csdn.net/handsomekang/article/details/9183303 Python字符串格式化--format()方法-----https://b ...
- Python内置的字符串处理函数
生成字符串变量 str='python String function' 字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 连接字符 ...
- Python:常见操作字符串的函数
Python中提供了很多操作字符串的函数: string = "hello, my dear python!" string.capitalize() #将字符串中的第一个字母大写 ...
- python - 增强的格式化字符串format函数
语法 它通过{}和:来代替%. “映射”示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.form ...
- Python旅途——简单语法
1. 前言 在我们对环境以及pycharm安装好之后,我们就可以开始我们的Python之旅了,那么,我们学习一门语言应该如何开始呢?就像我们学习汉语一样,从abcd这些拼音学起,而对于我们Python ...
- mssql sql server 其它系统函数 parsename 点语法字符串分割函数应用简介
转自:http://www.maomao365.com/?p=4534 一. parsename函数功能简介 parsename函数的主要功能是:可以快速的使用”.”关键字分解字符串,并返回.分解后指 ...
- delphi简单单向字符串加密函数
delphi用久了有的时候得给密码加密一下,简单点就行了,这个函数还是不错的. const XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$8 ...
随机推荐
- PHP太怪了,in_array() ,strpos,
PHP中在某个字符中查找另外一个字符串,是否存在,用的是strpos,此函数用法,经常很多人用反了,正确的用法是strpos(string,search),strstr等,前面是原字符串,后面是要在原 ...
- hdu3342 拓扑序
题意:一个QQ群里面有一群大神,他们互相帮助解决问题,然后互相膜拜,于是有些人就称别人是他师父,现在给出很多师徒关系,问是否有矛盾 拓扑序,按师徒关系建边直接拓扑序就行了. #include<s ...
- json对象和字符串互相转换
- kuangbin_UnionFind C (HDU 1213)
过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...
- Android Studio + gradle多渠道打包
通过工具栏的Build->Build Apk 好像只能打包第一个Module(eclipse里面是Project的概念),怎么多渠道打包呢?目前好像只能一个一个的打 首先在清单文件里设置个变量: ...
- freeswitch 1.4
yum install git gcc-c++ autoconf automake libtool wget python ncurses-devel zlib-devel libjpeg-devel ...
- 谷歌浏览器chrome与firefox的冲突(未解之谜)
那年,公司开发了一套在线制作电子书的系统 e-textbook. 我负责小学电脑科教材在线题目的制作. 利用 ps制作剪裁好图片,导入系统,制作题目,并通知同事添加代码. 检测时,却发现有一道图片拖放 ...
- Caffe-windows上训练自己的数据
1.数据获取 在网上选择特定类别,下载相应的若干张图片.可以网页另存或者图片下载器.本例中保存了小狗.菊花.梅花三类各两百多张. 2.重命名 import os import os.path root ...
- syslog-ng 安装
下载 Syslog-NG的rpm包, 地址 http://www.kevindeng.org/wp-content/uploads/2010/10/Syslog-NG.zip unzip解压 [ro ...
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist ,mysql 赋给用户权限 grant all privileges on
mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法 遇到了 SQLException: acce ...