第二章Python数字、字符串
本节所讲内容:
1.2.1 Python数字
1.2.2 python字符串
1.2.1 Python数字
数字:是python当中使用比较广泛的数据类型
Python 2
整形 Int 1
浮点型 Float 2.1
长整型 Long
Python 3
2版本长整型 long取消了
整形 Int 1
浮点型 Float 2.1
数字的运算:
+、 -、 * 、/、\\、%、
例:
>>> 3/2
1.5
>>> 3//2
1
>>> 3%2
1
>>> 3/2.0
1.5
>>>
**幂操作符 操作符左边一元运算符高于幂运算操作符,如果操作符右边一元运算操作符第一幂运算炒作符!
-3**2= -9
3**-2=0.1111111111111111
1.2.2 python字符串
字符串是一个元素以引号包围的,有序的,不可修改的序列。
字符串的定义:
‘’ 单引号
“” 双引号
>>> print(type("1"))
<class 'str'>
>>> print(type('1'))
<class 'str'>
>>> print("1")
1
>>> print('1')
1
>>> 'I'm a teacher'
SyntaxError: invalid syntax
>>> "I'm a teacher"
"I'm a teacher"
>>>
‘’’三单引号
“””三双引号
>>> "I'm a teacher"
"I'm a teacher"
>>> """
I am teacher
hello world
"""
'\nI am teacher\nhello world\n'
>>> '''
I am teacher
hello world
'''
'\nI am teacher\nhello world\n'
>>>
str类型函数
str(1)
'1'
由于python对脚本当中没有进行任何处理的字符串不进行编译,所以我们通常会用三引号作为长注释,多行注释
当一个字符串没有任何处理的出现在脚本、函数、类下的第一行,那么这个时候这个字符串被称为文档字符串
字符串当中的特殊字符
特色字符就是在字符串当中起到特殊含义的字符
|
\(在行尾时) |
续行符 |
|
\\ |
反斜杠符号 |
|
\' |
单引号 |
|
\" |
双引号 |
|
\a |
响铃 |
|
\b |
退格(Backspace) |
|
\e |
转义 |
|
\000 |
空 |
|
\n |
换行 |
|
\v |
纵向制表符 |
|
\t |
横向制表符 |
|
\r |
回车 |
|
\f |
换页 |
>>> print("a\nb")
a
b
>>> print("a\\nb")
a\nb
>>> "a\
b"
'ab'
>>>
字符串格式化操作:
在字符串当中以指定的格式符号进行占位,然后我们将指定的数据传入字符串
%s 字符串占位符
%d 数字占位符
%f 浮点型数字占位符
%.2f 控制浮点型数字占位符
例:
>>> "%s is %d years old"%("while",18)
'while is 18 years old'
>>> "%s is %d years old"%("mk",18)
'mk is 18 years old'
>>> "%s is %f m "%("mk",1.8)
'mk is 1.800000 m '
>>> "%s is %.2f m "%("mk",1.8)
'mk is 1.80 m '
>>>
字符串的索引(index)
在python当中所有有序的序列都是由索引概念的。
索引在我们初学的时候我们可以理解为字符串的下标
‘while’
|
w |
h |
i |
l |
e |
|
0 |
1 |
2 |
3 |
4 |
取单个元素
>>> 'while'[1]
'h'
>>> 'while'[0]
'w'
>>> 'while'[-1]
'e'
>>>
截取:
不包含结尾
>>> 'while'[:]
'while'
>>> 'while'[1:3]
'hi'
>>>
步长截取
>>> 'while'[::]
'while'
>>> 'while'[::2]
'wie'
>>> 'while'[::-1]
'elihw'
>>> 'while'[1:3:-1]
''
>>> 'while'[3:1:-1]
'li'
>>>
字符串的方法
|
字符串的修饰 |
Center |
让字符串在指定的长度居中,如果不能居中左短右长,可以指定填充内容,默认以空格填充 |
|
ljust |
让字符串在指定的长度左齐,如果不能居中左短右长,可以指定填充内容,默认以空格填充 |
|
|
rjust |
让字符串在指定的长度右齐,如果不能居中左短右长,可以指定填充内容,默认以空格填充 |
|
|
zfill |
将字符串填充到指定的长度,不足地方用0从左开始补充 |
|
|
strip |
默认去除两边的空格,去除内容可以指定 |
|
|
rstrip |
默认去除右边的空格,去除内容可以指定 |
|
|
lstrip |
默认去除左边的空格,去除内容可以指定 |
"while".center(10)
' while '
>>> "while".center(10,"x")
'xxwhilexxx'
>>> "while".ljust(10,"x")
'whilexxxxx'
>>> "while".rjust(10,"x")
'xxxxxwhile'
>>> "3".zfill(5)
'00003'
>>> "30".zfill(5)
'00030'
>>> " while ".strip()
'while'
>>> " while ".rstrip()
' while'
>>> " while ".lstrip()
'while '
>>> "xxxxxxwhilexxxxx".strip("x")
'while'
>>> "xxxxxxwhilexxxxx".ltrip("x")
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
"xxxxxxwhilexxxxx".ltrip("x")
AttributeError: 'str' object has no attribute 'ltrip'
>>> "xxxxxxwhilexxxxx".lstrip("x")
'whilexxxxx'
>>> "xxxxxxwhilexxxxx".rstrip("x")
'xxxxxxwhile'
>>>
|
字符串的查找 |
Count |
计数功能,返回自定字符在字符串当中的个数 |
|
Find |
查找,返回从左第一个指定字符的索引,找不到返回-1 |
|
|
rfind |
查找,返回从右第一个指定字符的索引,找不到返回-1 |
|
|
index |
查找,返回从左第一个指定字符的索引,找不到报错 |
|
|
rindex |
查找,返回从右第一个指定字符的索引,找不到报错 |
|
|
字符串的替换 |
replace |
从左到右替换指定的元素,可以指定替换的个数,默认全部替换 |
>>> "hello world".count("l")
3
>>> "hello world".find("l")
2
>>> "hello world".rfind("l")
9
>>> "hello world".index("l")
2
>>> "hello world".rindex("l")
9
>>> "hello world".find("x")
-1
>>> "hello world".index("x")
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
"hello world".index("x")
ValueError: substring not found
>>> "hello world".replace("l","L")
'heLLo worLd'
>>> "hello world".replace("l","L",2)
'heLLo world'
>>>
|
字符串的变形 |
upper |
将字符串当中所有的字母转换为大写 |
|
lower |
将字符串当中所有的字母转换为小写 |
|
|
swapcase |
将字符串当中所有的字母大小写互换 |
|
|
title |
将字串符当中的单词首字母大写,单词以非字母划分 |
|
|
capitalize |
只有字符串的首字母大写 |
|
|
expandtabs |
修改\t的长度 |
\t = tab键,是制表符,就是你每次使用tab,下 一次光标的位置在下一栏的起始位置,比如一般是一个tab四个空格位,如果你已经输入了两个字母,那么按下tab,光标的位置就来到了字母后面两个空格的位置,输出效果上"ab\tcd"和"ab cd"是一样的,所以这个expandtab也是这样,到上一个tab位或者行首一共数8个字符位,不够补齐空格
>>> print("a\tb")
a b
>>> print("ab\tb")
ab b
>>>
>>> "while".upper()
'WHILE'
>>> 'WHILE'.lower()
'while'
>>> "while123".upper()
'WHILE123'
>>> "wHile".swapcase()
'WhILE'
>>> "hellO worLd".title()
'Hello World'
>>> "a b".title()
'A B'
>>> "hellO worLd".capitalize()
'Hello world'
>>> "a\tb".expandtabs(4)
'a b'
>>> "a\tb".expandtabs(8)
'a b'
|
字符串的判断 |
isalnum |
判断字符串是否完全由字母和数字组成 |
|
isalpha |
判断字符串是否完全由字母组成 |
|
|
isdigit |
判断字符串是否完全由数字组成 |
|
|
isupper |
判断字符串当中的字母是否完全是大写 |
|
|
islower |
判断字符串当中的字母是否完全是小写 |
|
|
istitle |
判断字符串是否满足title格式(首字母大写) |
|
|
isspace |
判断字符串是否完全由空格组成 |
|
|
startswith |
判断字符串的开头字符,也可以截取判断 |
|
|
endswith |
判断字符串的结尾字符,也可以截取判断 |
>>> "hello123".isalnum()
True
>>> "hello123_".isalnum()
False
>>> "hello123?".isalnum()
False
>>> "hello123".isalpha()
False
>>> "hello".isalpha()
True
>>> "hello".isdigit()
False
>>> "1234".isdigit()
True
>>> "hello123".isupper()
False
>>> "hello123".islower()
True
>>> "hello world".istitle()
False
>>> "Hello World".istitle()
True
>>> " ".isspace()
True
>>> " \t\n ".isspace()
True
>>> " \t\n 1".isspace()
False
>>> "Hello World".endswith("d")
True
>>> "Hello World".endswith("a")
False
>>> "Hello World".startswith("H")
True
>>> "Hello World".startswith("h")
False
>>> "Hello World".endswith(3,5," ")
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
"Hello World".endswith(3,5," ")
TypeError: slice indices must be integers or None or have an __index__ method
>>> "Hello World".endswith(" ",3,5)
False
>>> "Hello World".endswith("o",3,5)
True
>>> "Hello World".startswith("o",4,5)
True
>>> "Hello World".startswith("o",4,6)
True
>>> "Hello World".startswith("o",4)
True
>>> "Hello World".endswith("o",3,5)
True
>>> "Hello World".endswith("o",2,5)
True
>>>
|
字符串的切分 |
splitlines |
以行切分字符串,可以指定是否保留行标志布尔值 |
|
split |
从左开始切分字符串,可以指定切分次数和对象 |
|
|
rsplit |
从右开始切分字符串,可以指定切分次数和对象 |
|
|
字符串的拼接 |
join |
将指定的字符串插入到后面的序列的每两个元素之间,进行拼接,形成一个新的字符串 |
|
+ |
将两个字符串拼接起来 |
|
|
* |
将指定的字符串进行重复 |
join:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
str = "-";
seq = ("a", "b", "c");
# 字符串序列
print str.join( seq );
#输出值
a-b-c