Python_基础_Day_1,,一、变量  由字母、


一、变量

  由字母、数字和下划线组成且不能以数字开头

二、标准数据类型

  五种类型:数字(Numbers)、字符串(String)、列表(List)、元组(Tuple)、字典(Dictionary)

三、数字类型

  1、四种类型:int(整型)、long(长整型)、float(浮点型)、complex(复数)

  2、基本模块:math模块、cmath模块

四、字符串类型

  1、字符串类型不可变(不能修改)

  2、字符串运算符

a = ‘hello‘b = ‘world‘print(a + b)  # helloworld,字符串连接print(a * 2)  # hellohello,重复输出字符串print(a[0:3]) # hel,截取,取头不取尾

  3、内置函数

  S.capitalize():把字符串的第一个字母转为大写

  S.casefold():把字符串的第一个字母转为小写

  S.center(width[, fillchar]):返回一个宽度为width,参数为fillchar(默认为空)的居中字符串

  S.count(sub, start=None, end=None):在start和end范围中查找子串sub在S中的数量

  S.encode(encoding=‘utf-8‘, errors=‘strict‘):默认返回一个utf-8编码的字符串

  S.startswith(self, prefix, start=None, end=None):在start和end范围中检查字符串S是否以prefix开头,是返回True,不是返回False

  S.endswith(self, suffix, start=None, end=None):在start和end范围中检查字符串S是否以suffix结尾,是返回True,不是返回False

  S.expandtabs(tabsize=8):把字符串中的 tab 符号(‘\t‘)转为空格

  S.find(sub, start=None, end=None):从左向右在start和end范围中检查子串sub是否在S中,在则返回起始索引值,不在则返回-1

  S.rfind(sub, start=None, end=None):从右向左查找

  S.index(sub, start=None, end=None):从左向右在start和end范围中检查子串sub是否在S中,在则返回起始索引值,不在报错

  S.rindex(sub, start=None, end=None):从右向左查找

  S.format(*args, **kwargs):格式化输出

  S.format_map(mapping)

  S.join(iterable)::把iterable序列中的元素用指定的S字符连接成一个新的字符串

  S.ljust(width[, fillchar]):以指定的宽度左对齐显示,默认以空格填补,宽度小于字符串的长度则显示原字符串

  S.rjust(width[, fillchar]):以指定的宽度右对齐显示,默认以空格填补,宽度小于字符串的长度则显示原字符串  

  S.replace(old, new[, count]):把字符串S中的old字符替换成new字符,如果没有指定count则全替换,否则按count数值来确定替换次数

  S.split(sep=None, maxsplit=-1):通过字符sep分割字符串返回一个列表,sep默认为空,maxsplit确定分割次数

  S.rsplit(sep=None, maxsplit=-1):从右向左分割

  S.splitlines([keepends]):按行分割

  S.strip([chars]):去除字符串str前后参数chars(默认为空格),返回去除后的新字符串

  S.lstrip([chars]):去除左边

  S.rstrip([chars]):去除右边

a = ‘hello‘b = ‘world‘c = ‘Abc‘# 1、capitalize、casefoldprint(a.capitalize()) # Helloprint(a) # helloprint(c.casefold()) # abcprint(c) # Abc# 2、centerprint(a.center(10))  #   helloprint(a.center(10,‘*‘)) # **hello***# 3、countprint(a.count(‘l‘)) # 2print(a.count(‘l‘,0,3)) # 1# 4、encodeprint(a.encode()) # b‘hello‘# 5、startswith、endswithprint(a.startswith(‘h‘)) # Trueprint(a.startswith(‘a‘)) # Falseprint(a.endswith(‘o‘)) # Trueprint(a.endswith(‘c‘)) # False# 6、expandtabsprint(‘a\tb‘.expandtabs()) # a       b# 7、find、index、rfind、rindexprint(a.find(‘a‘)) # -1print(a.find(‘h‘)) # 0print(a.rfind(‘h‘)) # 0print(a.index(‘h‘)) # 0#print(a.index(‘a‘)) # ValueError: substring not found# 8、joinprint(‘-‘.join([‘a‘,‘b‘,‘c‘])) # a-b-c# 9、rjust、ljustprint(a.ljust(10)) # helloprint(a.rjust(10)) #     hello# 10、replaceprint(a.replace(‘l‘,‘e‘)) # heeeo# 11、split、rsplit、splitlinesprint(a.split(‘e‘)) # [‘h‘, ‘llo‘]print(‘a\nb‘.splitlines()) # [‘a‘, ‘b‘]# 12、strip、lstrip、rstripprint(‘ a  ‘.strip()) # aprint(‘ a ‘.lstrip()) # aprint(‘ a ‘.rstrip()) #  a

  S.swapcase():大小写转换,大写换小写,小写转大写

str1 = "abc"str2 = "ABC"print(str1.swapcase()) #ABCprint(str2.swapcase()) #abc

  S.title():转为标题

  S.lower():转为小写

  S.upper():转为大写

s = ‘hello world‘print(s.title()) # Hello Worldprint(s.upper()) # HELLO WORLDprint(s.lower()) # hello world

  S.isalnum():如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回 False

s1 = "abcdef1"s2 = "b c"s3 = ""print(s1.isalnum()) #Trueprint(s2.isalnum()) #Falseprint(s3.isalnum()) #Fals

  S.isnumeric():与str.isnum功能相同

  S.isalpha():检测字符串是否只有字母,是则返回True,否则返回False

s1 = "abc"s2 = "a@c"print(s1.isalpha()) #Trueprint(s2.isalpha()) #False

  S.isdecimal()

  S.isdigit():检测字符串是否只有数字,是则返回True,否则返回False

s1 = "123"s2 = "a@c"print(s1.isdigit()) #Trueprint(s2.isdigit()) #False

  S.isidentifier()

  S.islower(self):字符串至少包含一个区分大小写的字符且都为小写,则返回True,否则返回False

s1 = "123"s2 = "a@c"s3 = "a1c"print(s1.islower()) #Falseprint(s2.islower()) #Trueprint(s3.islower()) #True 

  S.isprintable()

  S.isspace():检测字符串是否只有空格,是则返回True,否则返回False

s1 = " 3"s2 = " "print(s1.isspace()) #Falseprint(s2.isspace()) #True

  S.istitle():字符串中所有首字母大写则返回True,否则返回False

s1 = "And so On"s2 = "And So On"print(s1.istitle()) #Falseprint(s2.istitle()) #True

  S.isupper():字符串至少包含一个区分大小写的字符且都为大写,则返回True,否则返回False

s1 = "Ab"s2 = "A1@"print(s1.isupper()) #Falseprint(s2.isupper()) #True

  max(S):返回最大字符串

  min(S):返回最小字符串

s = "abca"print(max(s)) #cprint(min(s)) #a

  

  

Python_基础_Day_1

评论关闭