一、整数:

例如:1、10、30

整数可以做以下操作:

bit_length函数:返回该整数占用的最少位数:
>>> x=100
>>> x.bit_length()
7
位数值:
>>> bin(100)
'0b1100100 __abs__函数:返回绝对值:
__abs__<==> abs()
x= -100
y= 90
print("__abs__(x):", x.__abs__())
print("__abs__(y):", y.__abs__())
print("abs(x):", abs(x))
print("abs(y):", abs(y))
以上实例运行后反回结果结果为:
__abs__(x): 100
__abs__(y): 90
abs(x): 100
abs(y): 90 __and__函数:两值相加:
x.__add__(y) <==> x+y
x = 70
y = 90
print("__add__(x add y):", x.__add__(y))
print("+(x+ y):", x+y)
以上实例运行后反回结果结果为:
__add__(xadd y): 160
+(x+ y): 160 __cmp__函数:比较两数大小:
在python2.X中:
x.__cmp__(y) <==> cmp(x,y)
x = 70
y = 90
print("__cmp__(x cmp y):", x.__cmp__(y))
print("cmp(x,y):", cmp(x,y))
以上实例运行后反回结果结果为:
('__cmp__(x cmp y):', -1)
('cmp(x,y):', -1)
在python3.X中此函数已经取消,改成如下方法:
x = 70
y = 90
print ('(x > y) - (x < y):',(x > y) - (x < y))
以上实例运行后反回结果结果为:
(x > y) - (x < y): -1 __divmod__函数:两数相除,得到商和余数组成的元组:
x.__divmod__(y) <==> divmod(x, y)
x = 7
y = 3
print('__divmod__:', x.__divmod__(y))
print ('divmod:', divmod(x,y))
print ('divmod:', type(divmod(x,y)))
以上实例运行后反回结果结果为:
__divmod__: (2, 1)
divmod: (2, 1)
divmod: <class 'tuple'> __div__函数:两数相除:
x.__div__(y) <==> x/y
x = 7
y = 3
print(x.__div__(y))
print(x/y)
以上实例在python2.x运行后反回结果结果为:
x.__div__(y):2
x/y: 2
在python3.x中只能使用x/y运行后反回结果结果为:
x/y: 2.3333333333333335 __float__函数: 转换为浮点类型
x.__float__() <==> float(x)
x = 7
print('x.__float__():',x.__float__())
print('float(x):',float(x))
以上实例运行后反回结果结果为:
x.__float__(): 7.0
float(x): 7.0 __floordiv__函数:取两个数的商:
x.__floordiv__(y) <==> x//y
x = 7
y = 3
print('x.__floordiv__(y):',x.__floordiv__(y))
print('x//y:',x//y)
以上实例运行后反回结果结果为:
x.__floordiv__(y): 2
x//y: 2 __int__()函数:转换为整数
x.__int__() <==> int(x)
x = ""
print(type(x))
print(type(x.__int__()))
print(type(int(x)))
以上实例在python2.x运行后反回结果结果为:
<type'str'>
<type 'int'>
<type'int'>
在python3.x此函数会报错可以使init():
x = ""
print(type(x))
print(type(int(x)))
以上实例运行后反回结果结果为:
<class 'str'>
<class 'int'> __mod__()函数:取两值相除余数:
x.__mod__(y) <==> x%y
x = 7
y = 3
print('x.__mod__(y):',x.__mod__(y))
print('x%y:',x%y)
以上实例运行后反回结果结果为:
x.__mod__(y): 1
x%y: 1 __neg__()函数:取反值:
x = -7
y = 3
print('(x.__neg__():', x.__neg__())
print('(y.__neg__():', y.__neg__())
以上实例运行后反回结果结果为:
(x.__neg__(): 7
(y.__neg__(): -3 __sub__()函数:两数相减:
x = 7
y = 3
print('(x.__sub__(y):', x.__sub__(y))
print('x-y:', x-y)
以上实例运行后反回结果结果为:
(x.__sub__(y): 4
x-y: 4 __str__()函数:将整型转成字符串:
x = 8
print(type(x))
print('(x.__str__():', type(x.__str__()))
print('str(x)', type(str(x)))
以上实例运行后反回结果结果为:
<class 'int'>
(x.__str__(): <class 'str'>
str(x) <class 'str'>

整数(int)

二、字符串:

例如:'python'、'name'等:

字符串可以做以下操作:

capitalize()函数:首字母大写:
x = "python"
print(x.capitalize())
以上实例输出结果:
Python center()函数:使字符居中:
x = "python"
print(x.center(30, '#'))
以上实例输出结果:
############python############ count() 函数:计算某字符在字符串中出现的次数:
x = "this is string example....wow!!!"
print(x.count('i')) #字符i在整个字符串中出现的次数。
print(x.count('i', 4, 40)) #字符i在第4个字符与第40字符中出现的次数。
以上实例输出结果:
3
2 endswith()函数:判断是否以某字符结尾:
x = "this is string example....wow"
print(x.endswith("w")) #判断是否以w结尾。
print(x.endswith("w", 4, 6)) #判断4到6字符之间是否以w结尾。
以上实例输出结果:
True
False expandtabs()函数:将tab转成空格:
x = "this is\tstring example....wow."
print(x)
print(x.expandtabs())
print(x.expandtabs(20))
py
以上实例输出结果:
this is string example....wow.
this is string example....wow.
this is string example....wow. find()函数:寻找字符列位置,如果没找到,返回 -1:
x = "this is string example....wow."
print(x.find("i"))
print(x.find("w"))
print(x.find("q"))
以上实例输出结果:
2
26
-1 index()函数:寻找字符列位置,如果没找到,报错:
x = "this is string example....wow."
print(x.index("i"))
print(x.index("q"))
以上实例输出结果:
Traceback (most recent call last):
2
print(x.index("q"))
ValueError: substring not found isalnum()函数:判断字符串是否是字母和数字:
x = "this is string example....wow."
y = "this2016"
print(x.isalnum())
print(y.isalnum())
以上实例输出结果:
False
True isalpha函数:判断字符串是否全是字母:
x = "this is string example....wow."
y = "this2016"
z = "python"
print(x.isalpha())
print(y.isalpha())
print(z.isalpha())
以上实例输出结果:
False
False
True isdigit()函数:判断字符串是否全是数字:
x = "this is string example....wow."
y = "this2016"
z = ""
print(x.isdigit())
print(y.isdigit())
print(z.isdigit())
以上实例输出结果:
False
False
True islower()函数:判断是否全是小写:
x = "this is string example....wow."
y = "This 2016"
print(x.islower())
print(y.islower())
以上实例输出结果:
True
False isspace()函数:判断是否全是由空格组成:
x = "this is string example....wow."
y = " "
print(x.isspace())
print(y.isspace())
以上实例输出结果:
False
True istitle()函数:检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
x = "this is string example....wow."
y = "This Is String"
print(x.istitle())
print(y.istitle())
以上实例输出结果:
False
True isupper()函数:检测字符串中所有的字母是否都为大写
x = "This is string example....wow."
y = "THIS IS"
print(x.isupper())
print(y.isupper())
以上实例输出结果:
False
True join()函数:将列元组中的元素以指定的字符连接生成一个新的字符串:
x = "-"
y = ("a", "b", "c")
print(x.join( y ))
以上实例输出结果:
a-b-c ljust()函数:字符串左对齐:
x = "This is string example....wow."
print(x.ljust(50, '*'))
以上实例输出结果:
This is string example....wow.******************* rjust()函数:字符串右对齐:
x = "This is string example....wow."
y = "THIS IS"
print(x.rjust(50, '*'))
以上实例输出结果:
*******************This is string example....wow. lower()函数:将大写字符转小写:
x = "THIS IS book"
print(x.lower())
以上实例输出结果:
this is book strip()函数:删除两端空格:
x = " This is string example....wow. "
print(x.strip())
以上实例输出结果:
This is string example....wow. lstrip()函数:删除左则空格:
x = " This is string example....wow. "
print(x.lstrip())
以上实例输出结果:
This is string example....wow. rstrip()函数:删除右则空格:
x = " This is string example....wow. "
print(x.rstrip())
以上实例输出结果:
This is string example....wow. partition()函数:根据指定的分隔符将字符串进行分割:
x = "This is string example....wow."
print(x.partition("g"))
以上实例输出结果:
('This is strin', 'g', ' example....wow.') replace()函数:把字符串中的旧字符串,替换成新字符串,如果指定第三个参数max,则替换不超过 max 次。
x = "This is string example....wow."
print(x.replace("s", "w"))
print(x.replace("s", "w",2))
以上实例输出结果:
Thiw iw wtring example....wow.
Thiw iw string example....wow. split()函数:指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串
x = "This is string example....wow."
print(x.split('s'))
print(x.split('s', 1))
以上实例输出结果:
['Thi', ' i', ' ', 'tring example....wow.']
['Thi', ' is string example....wow.'] splitlines()函数:根据换行来分割:
x = "This \n is \n string example....wow."
print(x.splitlines())
以上实例输出结果:
['This ', ' is ', ' string example....wow.'] swapcase()函数:小写转大写,大写转小写:
x = "This Is String example....wow."
print(x.swapcase())
以上实例输出结果:
tHIS iS sTRING EXAMPLE....WOW.

字符串(str)

三、列表:

例如:['name','age','address']、[10,15,20]

列表可以做以下操作:

append()函数:在列表末尾添加新的对象:
x = ['name', 'age', 'address', '']
x.append('number')
print(x)
以上实例输出结果::
['name', 'age', 'address', '', 'number'] count()函数:统计某元素在列表里出现次数:
x = ['name', 'age', 'address', '', 'name']
print(x.count('name'))
print(x.count('age'))
以上实例输出结果::
2
1 extend()函数:在一个已经存在的列表末尾添加新的列表:
x = ['name', 'age', 'address', '', 'name']
y = ['pig', 'cat', '']
x.extend(y)
print(x)
以上实例输出结果::
['name', 'age', 'address', '', 'name', 'pig', 'cat', ''] index()函数:查找某个值在列表中第一次出现的索引位置:
x = ['name', 'age', 'address', '', 'name']
print(x.index("age"))
print(x.index(''))
以上实例输出结果::
1
3 insert()函数:将指定对象插入到指定位置:
x = ['name', 'age', 'address', '', 'name']
x.insert(2, "cat")
print(x)
以上实例输出结果::
['name', 'age', 'cat', 'address', '', 'name'] pop()函数:移除列表中的一个元素(默认最后一个元素),并且返回该元素的值,也可以指定移除指定的索引什所在的元素:
x = ['name', 'age', 'address', '', 'name']
print(x.pop())
print(x)
print(x.pop(2))
print(x)
以上实例输出结果::
name
['name', 'age', 'address', '']
address
['name', 'age', '','name'] remove() 函数:用于移除列表中某个值的第一个匹配项:
x = ['name', 'age', 'address', '', 'name']
x.remove('name')
print(x)
以上实例输出结果::
['age', 'address', '', 'name'] reverse() 函数:用于反向排序列表中元素:
x = ['name', 'age', 'address', '', 'name']
x.reverse()
print(x)
以上实例输出结果::
['name', '', 'address', 'age', 'name'] sort() 函数:用于对原列表进行排序:
x = ['name', 'age', 'address', '', 'name']
x.sort()
print(x)
以上实例输出结果::
['', 'address', 'age', 'name', 'name']

列表(list)

四、元组:

例如:('name','big','cat')、(10,15,20)

元组可以做以下操作:

count()函数:统计指定元素出现次数:
x = ('name', 'age', 'address', '', 'name')
print(x.count("name"))
以上实例输出结果::
2 index()函数:检索元素的索引值:
x = ('name', 'age', 'address', '', 'name')
print(x.index("age"))
以上实例输出结果::
1

元组(tuple)

注:元组的元素不能改变,但是元组内元素的元素可以改变。

五、字典:

例如:{'name': 'Earl', 'age': 26} 、{'ip': '1.1.1.1', 'port': 80]}

clear()函数:清除字典内的元素:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
dic.clear()
print(dic)
以上实例输出结果::
{} get()函数:返回指定键的值,如果值不在字典中返回默认值:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.get('name', 'phone'))
print(dic.get('age', 'phone'))
print(dic.get('number', 'phone'))
以上实例输出结果::
Eral
26
phone has_key()函数:如果给定的键在字典可用,has_key()方法返回true,否则返回false:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.has_key("name"))
以上实例输出结果:
true
注:此函数只在python2.x是有,在python3.x使用in函数,如下:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
if "age" in dic:
print("OK")
以上实例输出结果::
OK items() 函数:以列表返回可遍历的(键, 值) 元组数组:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.items())
for k, y in dic.items():
print("key:", k)
print("value:", y)
以上实例输出结果::
dict_items([('age', ''), ('address', 'jilin'), ('name', 'Eral')])
key: age
value: 26
key: address
value: jilin
key: name
value: Eral keys()函数:以列表返回一个字典所有的键
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.keys())
以上实例输出结果::
dict_keys(['address', 'name', 'age']) pop()函数:删除指定给定键所对应的值,返回这个值并从字典中把它移除:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.pop('age'))
print(dic)
以上实例输出结果::
26
{'address': 'jilin', 'name': 'Eral'} popitem()函数:随机返回并删除字典中的一对键和值:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.popitem())
print(dic)
以上实例输出结果::
('name', 'Eral')
{'age': '', 'address': 'jilin'}
注:由于字典是无序的所以是随机删除。 setdefault() 函数:此函数和get()方法类似, 如果键不存在于字典中,将会添加键并将值设为默认值:
dic = {'name': 'Eral', 'age': '', 'address': 'jilin'}
print(dic.setdefault('name', 'none'))
print(dic.setdefault('number', 'none'))
print(dic)
以上实例输出结果::
Eral
none
{'number': 'none', 'age': '', 'name': 'Eral', 'address': 'jilin'} update() 函数:把另一个字典的键/值对更新到dict里:
dic = {'name': 'Eral', 'age': ''}
dic2 = {'address': 'jilin'}
dic.update(dic2)
print(dic)
以上实例输出结果::
{'name': 'Eral', 'age': '', 'address': 'jilin'} values() 函数:是以列表返回字典中的所有值。
dic = {'name': 'Eral', 'age': ''}
print(dic.values())
以上实例输出结果::
dict_values(['Eral', 'jilin', ''])

字典(dict)

Python学习之路——字符处理(一)的更多相关文章

  1. Python学习之路——字符处理(二)

    一.set集合: set是一个无序且不重复的元素集合 建立一个集合: x = set([1, 'tom', 2, 3, 4]) print(type(x)) print(x) 以上实例运行后反回结果结 ...

  2. python学习之路-day2-pyth基础2

    一.        模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...

  3. Python学习之路-Day2-Python基础3

    Python学习之路第三天 学习内容: 1.文件操作 2.字符转编码操作 3.函数介绍 4.递归 5.函数式编程 1.文件操作 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个 ...

  4. Python学习之路-Day2-Python基础2

    Python学习之路第二天 学习内容: 1.模块初识 2.pyc是什么 3.python数据类型 4.数据运算 5.bytes/str之别 6.列表 7.元组 8.字典 9.字符串常用操作 1.模块初 ...

  5. Python学习之路-Day1-Python基础

    学习python的过程: 在茫茫的编程语言中我选择了python,因为感觉python很强大,能用到很多领域.我自己也学过一些编程语言,比如:C,java,php,html,css等.但是我感觉自己都 ...

  6. Python学习之路【第一篇】-Python简介和基础入门

    1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...

  7. python学习之路网络编程篇(第四篇)

    python学习之路网络编程篇(第四篇) 内容待补充

  8. python 学习之路开始了

    python 学习之路开始了.....记录点点滴滴....

  9. python学习之路,2018.8.9

    python学习之路,2018.8.9, 学习是一个长期坚持的过程,加油吧,少年!

随机推荐

  1. LRU算法的设计

    一道LeetCode OJ上的题目,要求设计一个LRU(Least Recently Used)算法,题目描述如下: Design and implement a data structure for ...

  2. Highlight On Mouseover Effect With JQuery

    How to get the xpath by clicking an html element How to get the xpath by clicking an html element Qu ...

  3. 使用Html5的DeviceOrientation特性实现摇一摇功能

    如今非常多的手机站点上也有类似于微信一样的摇一摇功能了,比方什么摇一摇领取红包,领取礼品等等 1,deviceOrientation:封装了方向传感器数据的事件,能够获取手机静态状态下的方向数据,如手 ...

  4. 虎记:强大的nth-child(n)伪类选择器玩法

    写在前面的戏: 最近参加了度娘前端IFE的春季班,刷任务,百度真是有营销头脑,让咱们这帮未来的技术狂人为他到处打广告(我可不去哪),其中做的几个任务中有几个以前没有用到的东西, 也算是有些许收获(现在 ...

  5. JavaScript 深入学习及常用工具方法整理 ---- 01.浮点数

    在JavaScript中是不区分整数值和浮点数值的,其中所有的数字均用浮点数值表示.JavaScript采用IEEE 754标准(有兴趣可以浏览网络规范分类下的IEEE 754标准,需要原文件请在留言 ...

  6. 关于CDH5.2+ 添加hive自定义UDAF函数的方法

  7. java 简单的词法分析

    package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...

  8. 利用python进行数据分析之pandas库的应用(二)

    本节介绍Series和DataFrame中的数据的基本手段 重新索引 pandas对象的一个重要方法就是reindex,作用是创建一个适应新索引的新对象 >>> from panda ...

  9. selenium + python自动化测试环境搭建--亲测

    环境准备: 1.下载所学安装包: setuptools https://pypi.python.org/packages/2.7/s/setuptools/ selenium https://pypi ...

  10. 不用注册热键方式在Delphi中实现定义快捷键(又简单又巧妙,但要当前窗体处在激活状态)

    第一步:在要实现快捷键的窗体中更改属性“KeyPreview”为True:第二步:在要实现快捷键的窗体中的OnKeyPress事件中填入一个过程名称(在Object Inspector中),填写好后回 ...