for循环:
range函数可以返回指定的从xx到xx之间的整数
for x int range(1,11)#range返回一个链表返回指定区间的数前面是开区间后面的闭区间[1,11)
if x==5:
continue;
print x;
while 循环打印99乘法表
i = 1
j = 1
while i<=9:
j=1
while j<=i:
print('%d*%d=%d' %(i,j,i*j),end=' ')#另输出的末尾是空格而不是换行'\n'
j+=1
i+=1
print()
for循环写99乘法表
for i in range(1,10):#range会产生一个链表起到了遍历的作用
for j in range(1,i+1):
print('%d*%d=%d' %(i,j,i*j),end=' ')
print()
Python-基础-字符串
什么是字符串:
- 在python中使用单引号或则双引号包括起来的,就是字符串。
greet = 'abc\
def'#\起到了连接字符的作用
print greet
#abcdef
- 字符串也可以使用6个双引号, 6个单引号括起来。
greet = '''
eeeee
aaaa aaaa'''
print(greet)
# eeeee
#aaaa aaaaa所进即所得
- 在python2中,字符串分为两种类型,第一种是str,第二种是unicode,他们都是继承自basestring
- 在python3中,字符串分为两种类型,第一种是str,也是python2中unicode,第二中是bytes,就是python2中的
str类型,是一种字节码。 - 字符串的拼接
a = 'hello'
b = 'world'
c = a + b
print (c)
# hello world
a = 'hello'
b = 'world'
c = '%s%s%s'%(a,b,a)
print (c)
# helloworldhello通过格式化的方式完成字符串你的拼接
- 取字母
username = 'zhiliaofsdfsds'
a = username[-1]# 取-1就相当于是最后一个字符
print a
# s
字符串的切片:
numbers ='123456789'
temp = numbers[0:3]
print temp
#123
字符串的索引值可以去负
username = '13243546'
temp = username[-4:]
print temp
#3546
步长的修改
numbers = '123456789'
temp = numbers[0::2]
print temp
#1 3 5 7 9 没有取步长默认为1
步长为正数代表方向是从左到右,如果步长为负数,那么方向从右到左
numbers = '123456789'
temp = numbers[1::2]
print (temp)
#2 4 6 8//从下标为1开始每次取两个
字符串的常用方法:
- find():返回查找的字符串首字母的下标,如果没有找到则返回-1.
username = 'hello word'
index = username.find('llo')
print index
#2
- index 方法:
和find函数具有几乎同样的作用,不过在没有找到目标字符串的时候回返回错误。
username = '12245553'
Index = username.index('22')
print (Index)
#2
username = '12324355'
Index = username.index('666')
print (index)
#ValueError 产生异常
- len函数用来获取字符串的长度
username = '123456'
length = len(username)
print (length)
# 6
- count函数用来获取某个字母出现的次数
username = 'python python 153'
temp = username.count('python')
print temp
# 2
username = '123456 python'
new_username = username.replace('python','789')
print new_username
#123456 789(注意:原函数没有发生改变)
- split函数
username = '2246 37535 3685'
username_list=username.split(' ')
print (username_list)
# ['2246','37535','3685']它的输出是一个列表的形式
- startswith (判断某个字符串是否是以某一个字符串开头的返回值true,false)
username = 'python 3 is very good'
result = username.startwith('python')
print (result)
#True
- endswith(判断是否是以某一个字符串结束)
username = 'python 3 and python'
result = username.endswith('python')
print (result)
# True
例子:
判断用户上传文件是否是照片:
/jpg/png/gif
file_name = 'xxx.js'
if file_name.endswith('jpg') or file_name.endswith('png') or file_name.endswith('gif')
print('该文件为图片')
else
print('该图片不是图片')
- lower (大写字母转小写字母)
username = 'PYTHON 132435'
username_lower = username.lower()
print(username)
print(username_lower)
#PYTHON 132435
#python 132435
比如输入验证码:
验证码一般都是支持大小写的。就是在后台匹配时会将用户输入的字母转化为小写字母再匹配
- upper 小写转大写(同上)
- strip函数用于出掉字符串前面和后面的空格
phone = ' 13739*5*2*2 '
new_phone = phone.strip()
print len(phone)
print len(new_phone)
#16
#11
lstrip()把左边的空格去掉,rstrip()函数把右边的空格去掉
如果要把字符串里面的所欲空格去掉可以用replace函数把所有的空格替换为空
username = ' pytho n3'
new_namespace = username.replace(' ','');
print new_namespace
#python3
- isalnum(判断字符串里面是否有除英文字母和数字意外的其他字符如果有就返回False没有就但会True)
username = 'uwaajg_25235'
temp = username.isalnum()
print temp
#False
isalpha()判断字符串是否全是英文
isdigit()判断字符串是否全是十进制数(True or False)
isspace()如果字符串中全是空格则返回True否则返回False
format函数(格式化)
1.最简单的使用方式
name ='tang'
age = 18
greet = 'my name is {},my age is {}'.format(name,age)
print greet
2.使用位置 i 函数的参数方式
name = 'tang'
age = 18
greet = 'my age is {0},my username is {0},my age is {1}'.format(name,age)
print (greet)
#字符串里面的花括号也起到了占位的作用同时他指定了要使用的后面的括号里面的那个参数(参数从左到右0~N)
- 关键字操作用关键字代替字符中的位置
name = 'python'
age = 18
greet = 'my name is {userage},myuserage is {userage},my sge is{age}'.format(userage=name,age=age)
print greet
#my name is name,myueerage is name,my age is age
dir函数可以把一个类中所有的函数输出出来:
all_mehtods = dir(str)
print(all_mehtods)
字符串中常用的转移字符:
转移字符 | |
---|---|
\在行尾 | |
\n | |
\ * | |
\t | |
\ \ |
username = 'tang\
123 '
print username
# tang 123
#用于字符串过长我们放在两行来处理
username = 'tang\n123'
print username
#tang
#123//回加上一个换行符
如果我们想让单 引号/双引号 出现在字符串里面
temp = " i'm python"
print temp
#i'm python
或者
username = 'i am \"python \"'
print username
#i am "python"使用方法在引号之前加上\
\t的使用方法:(制表符)即table键
temp = 'hello\tworld'
print temp
#hello world
\的使用
temp = 'helle\\nworld'
print (temp)
#hello\nworld
#第一个反斜杠将后面那个\进行转移使得它不在有换行的作用
原生字符串
#raw:原生字符串,是什么样子就是什么样子
#a = r'a\nb'
#print a
temp = r"hello\n\tworld"#原生字符串
print temp
#
为什么要用unicode字符串
utf-8->unicode->gbk
一个字符串转化为ucicode的方法有两种方式:
1.
name_unicode = name.decode('UTF-8')#通过UTF-8解码
print type(name_unicode)
#<知了,'UTF-8'>