首页 > 技术文章 > python DAY010(数字、字符串)

sunjj 2018-10-17 14:23 原文



编程语言
高级
低级

Python种类
JavaPython
cPthon ******
pypy
字节码 和 机器码

Python程序:
1.
终端:
C:\python35\python.exe D:\1.py
解释器:
C:\python35\python.exe

2.文件形
#/usr/bin/u/ubv/a python
python 1.py 加权限

3.编码
#/usr/bin/u/ubv/a python
# _*_ coding:utf-8 _*_
补充:

字节,位
unicode utf8 gbk
uft8: 3
gbk : 2

4.print('sdf')

5.inp = input('>>>')

PS:数字类型(字符串和数字) 字符串要带'引号',数字什么都不用加
>>> hello
inp = 'hello'

>>>10
inp = '10' inp/input 后面永远是字符串不是数字
inp * 10 = 10101010101010101010

#如果将字符串转换成数字 new_inp = int(inp)

6.变量名
字母
数字
下划线

要求:
不能以数字开头
不能使用关键字
不建议使用python内置的内容 eg. sum

7.条件语句
1.基本
2.嵌套
3. if elif else

8.while 循环
while 条件:
...
print('...')

补充:
a. while else
b. continue break
continue,终止当前循环,开始下一次循环
break ,终止所有循环



今日内容:
python开发IDE: python、eclipse

#专业版
#不要汉化

1、运算符
+ - * / ** % //

判断某个东西是否在某个东西里面包含
in not in 结果=布尔值
== 等于
> 大于
< 小于
>= 大于等于
<= 小于等于
!= 不等于
<> 不等于
not 非 v=not False

数字
字符串
布尔值:
True 真
False 假


name = '江苏省'
#"江苏省" 字符串
#"江" 字符
# "江苏省" 江苏 子字符串,子序列


if "江苏" in anme:
print('ok')
else:
print('Error')



v = user == 'alex' and pwd = '123' or 1 == 1
v = user == 'alex' or pwd = '123'
v = not False

补充:
先计算括号内
执行顺序:
从前到后
结果
True OR ==> True
True AND ==> 继续走
True AND ==> 继续走
Flase AND ==> False

# user = "alex"
# pwd = "123"
# user == 'alex' and (pwd = '123' or 1 == 1)
#
# user == 'alex' and pwd = '123' or 1 == 1 and pwd = '3389'
#
# v = user == 'alex' and pwd == "123" or 1==1 and pwd == "3389"


运算符
结果是值
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1

结果是布尔值
比较运算
a = 1>5
逻辑运算
a = 1>6 or 1==1
成员运算
a = '姜' in '江苏省'


2.基本数据类型
数字 int , 所有的功能,都放在int里
a1 = 123
a2 = 456

- int
将字符串转换为数字
a = '123'
b = int(a)
b = b + 1000

a = '123'
print(type(a),a) #输出a的类型和数值

b = int(a) #将a转换成10进制
print(type(b),b) #输出b的类型,再输出b的数值

num = "0011"
v = int(num, base=8) #以8进制的方式把unm转换成10进制
print(v)
- bit_lenght
#当前数字的二进制,至少用n位表示
age = 5
r = age.bit_length()
print(r)


字符串 str
s1 = 'asdf'
s2 = 'asdffas'


test = "aLex"
#首字母大写
v = test.capitalize()
print(v)


# 所有变小写,casefold功能比较强大,很多未知的相对应变小写
v1 = test.casefold() #所有字母变小写
print(v1)

v2 = test.lower() #所有字母变小写
print(v2)


test = "sunjj"
v = test.center(20)
print(v)
#设置宽度,并将内容剧中
#20代指总长度
# * 空白未知填充,一个字符,可有可无,参数选项后有=号的参数可有可无,没有=号的必须加参数。
v = test.center(20,"中")
print(v)


test = "alex"
v = test.ljust(20,"*")
print(v)
#将*填充在字符右边

test = "sunjj"
v = test.rjust(20,"@")
print(v)
#将@填充在字符左边


test = "alex"
v = test.zfill(20)
print(v)
#默认填充0,不能修改,以上功能都能实现,一般不用这个功能。






test = "aLexalex"
v = test.count('x',5,6)
print(v)
#在字符串中寻找子序列x 出现的次数. 以第5个字符开始第6个字符结束。



# 欠
# encode
# decode


test = "alex"
v = test.endswith(ex)
v = test.startswith(a)
print(v)
#以什么什么结尾
#以什么什么开始




test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
v = test.expandtabs(20)
print(v)
#expandtabs,断句20,用空格补齐20个占位, \n 是换行。



test = "alexalex"
v = test.find('ex')
print(v)
# > 或 >= 位置永远是从0开始后后数 012345678
# 未找到显示-1
# 从开始往后找,找到第一个之后,获取其位置



test = "alexalex"
v = test.index('8')
print(v)
# index与find功能一样,找不到会直接报错 忽略(以后直接使用find就可以)



test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v)
#格式化,将一个字符串中的占位符替换为指定的值

test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v)
#格式化,将一个字符串中的占位符替换为指定的值,以数字代替会按顺序进行替换占位符。


test ='i am {name}, age {a}'
v1 = test.format(name='df',a=10)
v2 = test.format_map("name":'alex',"a":19)
print(v1)
print(v2)
#格式化,传入的值{"name":"alex","a":19}


test = "uasf890_$%"
v = test.isalnum()
print(v)
#判断字符串中否只包含字母和数字



test = "as2df"
v = test.isalpha()
print(v)
#判断是否为字母


test = "234"
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)
#判断输入的值是否是数字



#判断当前输入是否是数字
test = "二" #1,②
v1 = test.isdecimal() #最常用
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)



#\t 制表符
#\n 换行
test = "oiuas\ttdfkjsa"
v = test.isprintable()
print(v)
#判断是否存在不可显示的字符




a = "def"
v = a.isidentifier()
print(v)
#字母,数字,下划线:标识符 def class



test = " "
v = test.isspace()
print(v)
#判断是否全部为空格



test = "Return True if S is a titlecased string and there is at least one"
v = test.istitle()
print(v)
v2 = test.istitle()
print(v2)
v3 = v2.istitle()
print(v3)
#判断是否为标题以及变成标题



test = "江苏省徐州市"
print(test)
v = " ".join(test) ****重要
print(v)
#将字符串中的每一个元素按照指定分隔符进行拼接



test = "SunJJ"
v1 = test.islower()
v2 = test.lower()
print(v1,v2)
#判断是否全部为小写,并变成小写。

test = "SunJJ"
v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
#判断是否全部为大写,并变成大写。



test = "xalex"
v = test.lstrip('xa')
v = test.rstrip('9lexxexa')
v = test.strip('xa')
print(v)
#移除指定字符串
#优先最多匹配


test = "\nalex"
test.lstrip()
test.rstrip()
#去除左右空白
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
#默认可以去除空白 \n \t



v = "asfiejsleowaeouilksdf;qpisfak;dieowjfu"
m = str.maketrans("aeiuo","12345")
new_v = v.translate(m)
print(new_v)
#进行字符替换



test = "testasdsddfg"
v = test.partition('s') #以s进行分割
print(v)
v = test.rpartition('s') #从右边开始以s进行分割
print(v)


v = test.split('s',2)
print(v)
test.rsplit('s')

# 正则表达式
# 是否想要分割的元素
# v = test.split('s',2)



#分割,只能根据,true,false: 是否保留换行
test = "asdfsdfsaf\nadfweadfsdfe\nsfeadfaf"
v = test.splitlines(True)
print(v)


test = "Sunjj"
v = test.swapcase()
print(v)
#大小写转换


test = "sunjjsunjjsunjjsunjj"
v = test.replace('un',"ab")
print(v)
v = test.replace('un','ab'2)
print(v)
#字符串替换

"alex"
"uilex"
# le 是最长公共子序列

############################ 7个基本字符串,重要#####################
join()
split()
find()
strip()
upper()
lower()
replace



############################ 4个灰色字符串 ##############################
test = "徐州市人民政府官方网站"
index = 0
while index < len(test):
v = tset[index]
print(v)
index += 1
print("end")

#for循环
test = "徐州市人民政府官方网站"
for v in test
print(v)





test = "alex"
v = test[3]
print(v)
#索引,下标,获取字符串中的某一个字符
v = len(test) #len 获取当前字符串中由几个字符组成
print(v)


切片
v = test[0:-1] # <=0 >1
print(v)



len("asdf")
'_'.join(asdfasdf)


test = "江苏省徐州市政府官方网站"

for item in test:
print(item)
break

for item in test:
continue
print(item)

#帮助创建连续的数字
v = range(100) # >=0 <100
v = range(0,100,5) #0,100 是范围 5是步长。
for item in v:
print(item)



练习题:

test = input('>>>')
for item in test:
print(item)

#将文件对应的索引打印出来:

test = input('>>>')
print(test) #test = qwe test[0] test[1]
l = len(test) #l = 3
print(l)

r = range(0,l) # 0,3
for item in r:
print(item,test[item]) # 0 q, 1 w, 2 e


test = input('>>>')
for item in range(0,len(test)):
print(item,test[item])



注意
len('asdf')
for循环
索引
切片


#######################记住以上10个字符串###################



#字符串一旦创建就不可修改;
#一旦修改或者拼接,都会造成重新生成字符串。

# name = 'sunjjingjing'
# age = '18'
# info = name + age
# print(info)


推荐阅读