基本数据类型--数字、字符串

2.数字

int

没有数字大小的限制,没有long的类型,都只是整形int(在pyCharm中查看数据类型的方法,按住Ctrl不放,鼠标指针放在数据类型上,左键点击)
a = "123"
print(type(a),a)     #type(variable)  #查看这个变量的类型

b = int(a)
print(type(b),b)

num = "0011"   #16进制表示
v = int(num, base=16)   #base这个表示将16进制表示的数字转换为10进制
print(v)

r = age.bit_length() # bit_lenght当前数字的二进制,至少用n位表示,就是有多少位2进制数(不是字节)

number = 1234
string = str(number)    #将数字转换为字符串
number = int(string)   #将字符串转换为数字

float

float是浮点数的意思,也就是有小数部分
#浮点数
number = 5.3
print(type(number),number)

复数

python重点复数的表示方法如下:(4+4j)
复数一般在某些特定领域需要,一般程序员不会涉及到。


3.字符串

注意
- 字符串一旦创建就不能在被修改,只能另外申请空间储存修改后的字符串。
- 字符串的形式:
- n1 = "alex" n2 = 'root' n3 = """eric""" n4='''tony''' 只要是引号都可以,同时””” “”” 既可以用于字符串,亦可以用于多行注释

字符串常见基本内置函数

s1 = "asdf"
s2 = "asdffas"
test = "aLex"


v = test.capitalize()  # 首字母大写
v1 = test.casefold()   # 所有变小写,casefold更牛逼,很多未知的符号对相应变小写
print(v1)
v2 = test.lower()      # 英文中所有变小写
print(v2)

# 设置宽度,并将内容居中
v = test.center(20<代指总长度>,"*"<空白未知填充,只能写一个字符,可有可无>)
#  原函数def center(self<表示自身,不用使用>,width<必须写>,fillchar=None<可选项>)
print(v)

v = test.ljust(20,'*')  #与center 功能类似,放在左边
v = test.rjust(20,'*')  #与center 功能类似,放在右边

test = "aLexalexr"
v = test.count('ex')   # 去字符串中寻找,寻找子序列的出现次数
print(v)

test = "aLexalexr"
v = test.count('ex',5,6)   # 5这里的位置表示从0开始到第五个开始,6这里的位置表示结束位置,后面两个位置可选,结束位置不包含 [),机制和java一样
print(v)

# 以什么什么结尾
# 以什么什么开始
test = "alex"
v = test.endswith('ex')   #结果是布尔值
v = test.startswith('ex')   #结果是布尔值
# print(v)

# test = "username\temail\tpassword\n邓晓庆\t123@.com\t123\n"    # \为转义字符, \t制表符
# v = test.expandtabs(6)    #以括号内的指定参数个数进行排列,如果遇到\t,自动补齐空格到参数个。
# print(v,len(v))


test = "alexalex"
v = test.find('ex')  从开始往后找,找到第一个之后,获取其位置,可以指定开始和结束的位置,结束的位置不包含 [ ),如果未找到返回-1,机制和java一样
print(v)

test = "alexalex"
v = test.index('8')   #找到对应的字符串,返回其下标,如果没有则抛出异常
print(v)


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)

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

test = "123"
v = test.isalnum()  # 字符串中是否只包含字母,数字。
print(v)

test = "as2df"
v = test.isalpha()  # 是否是字母,汉字
print(v)

# 13 当前输入的字符串是否是数字
test = "二"   # 1,②
v1 = test.isdecimal()  # 当前输入的字符串是否是数字,只支持十进制数
v2 = test.isdigit()     # 当前输入的字符串是否是数字,支持十进制,特殊数字字符
v3 = test.isnumeric()  #当前输入的字符串是否是数字,支持十进制,特殊数字字符,中文数字
print(v1,v2,v3)

#\t   制表符(不可显示)
#\n   换行(不可显示)
test = "oiuas\tdfkj"
v = test.isprintable()  # 是否存在不可显示的字符,如果含有,返回False
print(v)

# 15 判断是否全部是空格
test = ""
v = test.isspace()  #判断是否全部是空格,''这种不算空格
print(v)

# 16 判断是否是标题
test = "Return True if all cased characters in S are uppercase and there is"
# v1 = test.istitle()  #判断是否为标题(首字母全部大写)
print(v1)
v2 = test.title()   #将字符串转换为标题
print(v2)
v3 = v2.istitle()
print(v3)


#@@@@ ***** 将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"
print(test)
t = ' '
v = "_".join(test)  #  你_是_风_儿_我_是_沙,其他数据类型中的元素为字符串也可以(比如列表)
print(v)

# 18 转换为大小写
test = "Alex"
v1 = test.islower()   #判断是否全部是大小写
v2 = test.lower()    #转换为大小写
print(v1, v2)

v1 = test.isupper()   #判断是否全部是大小写
v2 = test.upper()    #转换为大小写
print(v1,v2)

# 19 If chars is given and not None, remove characters in chars #instead ,如果给定了一些非空的(''空白不属于非空),删除参数给定字符的字符
test = "xa"
v = test.lstrip('xa')   #默认移除左边\t \r \n 空白,有参数就从左到右找有没有对应的字符,有就删除,直到遇见参数没有的字符的就中断,
v = test.rstrip('9lexxexa')  #默认移除右边\t \r \n 空白,有参数就从右到左找有没有对应的字符,有就删除,直到遇见参数没有的字符的就中断,
v = test.strip('xa')   #默认移除两边\t \r \n 空白,有参数就从两边找,然后删除,直到遇见参数没有的字符的就中断,
print(v)

20 对应关系替换
test =  "aeiou"
test1 = "12345"

v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")   #创建对应关系,可用于替换
new_v = v.translate(m)
print(new_v)

# 21 分割为三部分
test = "testasdsddfg"
v = test.partition('s')    #从左边找到指定的字符串进行分割,分成三份,如果字符串在最开始,以‘’空代替。所以么有lpartition
print(v)
v = test.rpartition('s')   #从右到左找到指定的字符串进行分割,分成三份
print(v)

# 22 分割为指定个数
v = test.split('s',2)     #从左到右分割为指定个数,不加个数会将所有的分割,这种分割会去除指定字符串
print(v)
test.rsplit()   #从右到左分割为指定个数,不加个数会将所有的分割,这种分割会默认去除空格或空格操作


# 23 分割,只能根据,true,false:是否保留换行符
test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(False)
print(v)

#  24 以xxx开头,以xx结尾
est = "backend 1.1.1.1"
v = test.startswith('a')   #以什么什么开头,布尔值
print(v)
test.endswith('a’)       #以什么什么开头,布尔值

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

# 26 字母,数字,下划线 : 标识符 def  class
a = "def"
v = a.isidentifier()   #判断字符串能否被使用为标识符
print(v)

test = "alexalexalex"
v = test.replace("ex",'bbb')   #将指定字符串替换为指定字符串,全部换
print(v)
v = test.replace("ex",'bbb',2)  #将指定字符串替换为指定字符串,从左到右指定换参数个
print(v)

7个基本函数

  • join # '_'.join("asdfasdf")
  • split
  • find
  • strip
  • upper
  • lower
  • replace

4个关键函数

  • len()
  • for循环
  • 索引<下标>
  • 切片 #所有的数据类型都可以用
test = “alex”
v = test[0]    #取字符
t = test[0:2]    #取指定字符切片,不包括末尾指定的数
c = test[0:-1]    #从取字符切片,末尾倒着数,不包括末尾 (嵌套也可以取)
print(len(c))    #查看长度,有多少个字符,其他数据类型也可以,python2查看汉字会为3倍(utf8)

再次强调
字符串一旦创建,不可修改,一旦修改或者拼接,都会造成重新生成字符串

name = "zhengjianwen"
age = "18"
info = name + age
print(name,id(name))
print(age,id(age))
print(info,id(info))


4.字符串格式化

%d 十进制数字
%s 字符串(可接受任何值)
%f 浮点数

msg='i am %s my hobby is %s' % ('lhf','alex')  #多个值的时候要用括号
print(msg)

msg='i am %s my hobby is %s' % ('lhf',1)       #%s可以接受任何值
msg='i am %s my hobby is %s' % ('lhf',[1,2])   #接受列表
print(msg)

name='lhf'
age=19
msg='i am %s my hobby is %d' % (name,age)   #%d代表整型数字,也可接受浮点数(转换成int)
print(msg)

#打印浮点数
tpl = "percent %.2f" % 99.976234444444444444  #代表只保留后面两位小数
print(tpl)

#打印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444  #打印百分比
print(tpl)

tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}  #括号表示一个键key,后面传的为一个字典
print(tpl)

msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'} #()key -左对齐(+右对齐) 60宽度
print(msg)

msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}  # 加颜色
print(msg)


print('root','x','0','0',sep=':')   #sep以:产生一个记录
print('root'+':'+'x'+':'+'0','0')


5.format格式化

tpl = "i am {name}, age {age}, really {name}".format(name = "seven", age = 18)  #
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})  # **代表字典
tpl = "i am {:s}, age {:d}".format(*["seven", 18])   #*代表列表
tpl = "i am {:s}, age {:d}".format("seven", 18)     #["seven", 18]

l=["seven", 18]
tpl = "i am {:s}, age {:d}".format(*l)
tpl = "i am {:s}, age {:d}".format('seven',18)
print(tpl)

# 进制转换,后面的参数以10进制为准(x小写的16进制 X大写的16进制),百分数,参数少了不行,多了可以
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87, 2)
print(tpl)

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})  #**代表字典
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tpl = "i am {:s}, age {:d}".format(*["seven", 18])  #*代表传入的为列表
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)


6.数字,字符串相关的加减乘除

字符串加法:

n1 = "a"
n2 = "b"
n3 = "c"
n4 = n1 + n2 + n3   # "abc"

字符串乘法:

n1 = "a"
n2 = n1 * 3   # "aaa"

数字:

n1 = 9
n2 = 2

n3 = n1 + n2
n3 = n1 - n2
n3 = n1 * n2
n3 = n1 / n2
n3 = n1 % n2   # 求余
n3 = n1 ** n2  # 这个表示n1的n2次方
n3 = n1 // na  # 去除小数部分的值
posted @ 2018-06-27 17:46  哈哈大圣  阅读(456)  评论(0编辑  收藏  举报