当前位置:网站首页>【无标题】

【无标题】

2022-07-17 05:22:00 我是渣渣辉

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


1.访问[(1, 10), (2, 20), (3, 30)]列表中元组的每个元素

for i, j in [(1, 10), (2, 20), (3, 30)]:
    print(i, j)

2.打印9 * 9乘法表

# 第一种
for i in range(1, 10):
	j = 1
	for j in range(1, i + 1):
		print("{} * {} = {:^2}".format(i, j, i * j), end=" ")
	print()
# 第二种
i = 1
while i <= 9:
	j = 1
	while j <= i:
		print("{} * {} = {:^2}".format(i, j, i * j), end=" ")
		j += 1
	print()
	i += 1

3.运算符的使用:

算术运算符:所有的英文,以及使用样例

符号英文
+add
-sub
*mul
/div
%mod
//floordiv
**pow
print(1 + 1)
print(1 - 1)

比较运算符:所有的英文,以及使用样例

符号英文缩写
==equaleq
!=not equalne
>greater thangt
>=greater than equalge
<less thanlt
<=less than equalle

位运算符:&, |, ~, 使用的样例,以及二进制操作的过程

位运算符:操作的是二进制
&:按位与
|:按位或
~:按位取反
^: 按位异或
<<:左移
>>:右移

10 & 10 => 10
10 | 10 => 10
~10 => -11
10 ^ 10 => 0

1010
1010
 按位去与: 10 & 10
 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0
 1010 => 10
1010
1010
 按位去或: 10 | 10
1 | 1 = 11 | 0 = 1, 0 | 0 = 0
#1 | 0 或者 1 = 1
1010 =10
~10 =-11
01010
 按位取反: ~10, 对符号位也要取反
10101 =》 补码 -> 反码 -> 原码
10101 =》反码= 补码减一
10100 =》原码= 反码取反(除符号位之外的)
11011 =》原码-> -11

正数,原码,补码,反码 一致
负数:原码,补码,反码 不一致
       反码: 是对原码的取反
       补码: 反码 + 1

符号位:二进制最高位是我们的符号位。
       正数符号位为0
       负数符号位为1
<<: 左移
10 << 2
00001010 => 00101000 => 40
>>: 右移
10 >> 2 = >

4.if的三种形式,举样例说明

num = int(input("请输入一个数字"))
print("==========")
if num == 1:
	print("1")
print("==========")
if num == 1:
    print("1")
else: 
	print("2")
print("==========")
if num == 1:
	print("1")
elif num == 2:
	print("2")
else:
	print("3")

str字符串中的方法

      capitalize(self, /)
 |      Return a capitalized version of the string.
 |      # 返回字符串的大写版本。
		""" str_var = "one" print(str_var.capitalize()) # One """
 |      More specifically, make the first character have upper case and the rest lower
 |      case.
		# 更具体地说,使第一个字符具有大写,其余字符具有小写。 
casefold(self, /)
 |      Return a version of the string suitable for caseless comparisons.
 |      # 返回适合无大小写比较的字符串版本。(将字符串全部成小写)
		""" str_var1 = "one" str_var2 = "ONE" print(str_var1.casefold()) # one print(str_var2.casefold()) # one """
center(self, width, fillchar=' ', /)
 |      Return a centered string of length width.
 |      # 返回一个长宽居中的字符串。(如果为奇数,左边会比右边少一个字符)
		"""str_var1 = "one" print(str_var1.center(10, "*")) """
 |      Padding is done using the specified fill character (default is a space).
 |  	# 填充是使用指定的填充字符完成的(默认为空格)。
count(...)
 |      S.count(sub[, start[, end]]) -> int
 |      # 统计字符串中该字符串/字符出现的次数
 |      Return the number of non-overlapping occurrences of substring sub in
       # 返回子字符串sub在中不重叠的出现次数
		""" str_var1 = "abcabcabc" print(str_var1.count('abc')) print(str_var1.count('a')) print(str_var1.count('a', 3, 6)) """
 |      string S[start:end].  Optional arguments start and end are
    	# 字符串S[开始:结束]。可选参数start和end是
 |      interpreted as in slice notation.
 |  	# 解释为切片表示法。
encode(self, /, encoding='utf-8', errors='strict')
 |      Encode the string using the codec registered for encoding.
 |      # 使用注册用于编码的编解码器对字符串进行编码。
 |      encoding
 |        The encoding in which to encode the string.
        """ str_var2 = "中文" str_var3 = str_var2.encode() print(str_var3) # b'\xe4\xb8\xad\xe6\x96\x87' """
		# 对字符串进行编码的编码。
 |      errors
 |        The error handling scheme to use for encoding errors.
    	# 用于编码错误的错误处理方案。

 |        The default is 'strict' meaning that encoding errors raise a
    	# 默认值为“strict”,表示编码错误会引发
 |        UnicodeEncodeError.  Other possible values are 'ignore', 'replace' and
		# UnicodeEncodeError。其他可能的值包括“忽略”、“替换”和
 |        'xmlcharrefreplace' as well as any other name registered with
 |        codecs.register_error that can handle UnicodeEncodeErrors.
 |  	# “xmlcharrefreplace”以及注册于编解码器。register\u错误,可处理UnicodeEncodeErrors。
endswith(...)
 |      S.endswith(suffix[, start[, end]]) -> bool
 |      # 判断是否以指定后缀结尾
 |      Return True if S ends with the specified suffix, False otherwise.
		# 如果S以指定后缀结尾,则返回True,否则返回False。
		""" str_var1 = "abcabcabc" print(str_var1.endswith('abc')) print(str_var1.endswith('a')) print(str_var1.endswith('a', 6, 7)) """
 |      With optional start, test S beginning at that position.
    	# 使用可选启动,测试S从该位置开始。
 |      With optional end, stop comparing S at that position.
		# 使用可选结束,停止在该位置比较S。
 |      suffix can also be a tuple of strings to try.
		# 后缀也可以是要尝试的字符串元组。
      	# print(str_var1.endswith(("a", 'b', 'c')))
expandtabs(self, /, tabsize=8)
 |      Return a copy where all tab characters are expanded using spaces.
 |      # 返回使用空格展开所有制表符的副本。(在只有一个字表符时:tabsize 的默认值为8。tabsize值为0到7等效于tabsize=8。tabsize每增加1,原字符串中“\t”的空间会多加一个空格。在有多个字表符时,多出的字表符随着tabsize的值发生变化)
		""" str1 = "i love python" str = "i love\t\tpython" print(str1) print(str) print(str.expandtabs())#默认值为8 print(str.expandtabs(tabsize=8)) print(str.expandtabs()) print(str.expandtabs(2)) print(str.expandtabs(tabsize=2)) print(str.expandtabs(tabsize=9)) print(str.expandtabs(tabsize=10)) """
 |      If tabsize is not given, a tab size of 8 characters is assumed.
    	# 如果未给定tabsize,则假定tab大小为8个字符。
 |  
find(...)
 |      S.find(sub[, start[, end]]) -> int
 |      
 |      Return the lowest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      # 返回在S中找到子字符串sub的最低索引,
		# 使sub包含在S[开始:结束]中。可选择的
		# 参数start和end被解释为切片表示法。
		""" str_var1 = "abcabcabc" print(str_var1.find('abc')) print(str_var1.find('a', 2, 6)) """
 |      Return -1 on failure.
 |  	# 故障时返回-1。
format(...)# 格式化字符串统一讲
 |      S.format(*args, **kwargs) -> str
 |      
 |      Return a formatted version of S, using substitutions from args and kwargs.
    	# 使用args和kwargs中的替换返回S的格式化版本。
 |      The substitutions are identified by braces ('{' and '}').
		# 替换由大括号(“{”和“}”)标识。
 |  
format_map(...)
 |      S.format_map(mapping) -> str
 |      
 |      Return a formatted version of S, using substitutions from mapping.
 |      The substitutions are identified by braces ('{' and '}').
 |  
index(...)
 |      S.index(sub[, start[, end]]) -> int
 |      
 |      Return the lowest index in S where substring sub is found,
 		# 返回在S中找到子字符串sub的最低索引,
		""" str_var1 = "abcabcabc" print(str_var1.index('abc')) print(str_var1.index('a', 2, 6)) """
 |      such that sub is contained within S[start:end].  Optional
		# 使sub包含在S[开始:结束]中。可选择的
 |      arguments start and end are interpreted as in slice notation.
 |      # 参数start和end被解释为切片表示法。
 |      Raises ValueError when the substring is not found.
		# 找不到子字符串时引发ValueError。
isalnum(self, /)
 |      Return True if the string is an alpha-numeric string, False otherwise.
 |      # 如果字符串是字母数字字符串,则返回True,否则返回False。
		""" str_var1 = "qwe12" str_var2 = "qwe" str_var3 = "123" str_var4 = "+++" print(str_var1.isalnum()) # True print(str_var2.isalnum()) # True print(str_var3.isalnum()) # True print(str_var4.isalnum()) # False """
 |      A string is alpha-numeric if all characters in the string are alpha-numeric and
 |      there is at least one character in the string.
 |  	# 如果字符串中的所有字符都是字母数字,并且字符串中至少有一个字符。
isalpha(self, /)
 |      Return True if the string is an alphabetic string, False otherwise.
 |      # 如果字符串是字母字符串,则返回True,否则返回False。
   		""" str_var3 = "123" str_var2 = "qwe" print(str_var3.isalpha()) # False print(str_var2.isalpha()) # True """

 |      A string is alphabetic if all characters in the string are alphabetic and there
 |      is at least one character in the string.
		# 如果字符串中的所有字符都是字母,并且字符串中至少有一个字符。
isascii(self, /)
 |      Return True if all characters in the string are ASCII, False otherwise.
 |      # 如果字符串中的所有字符都是ASCII,则返回True,否则返回False。
		""" str_var1 = "49" str_var2 = "中文" print(str_var1.isascii()) # True print(str_var2.isascii()) # False """
 |      ASCII characters have code points in the range U+0000-U+007F.
    	# ASCII字符的代码点范围为U+0000-U+007F。
 |      Empty string is ASCII too.
 |  	# 空字符串也是ASCII。
isdecimal(self, /)
 |      Return True if the string is a decimal string, False otherwise.
 |      # 如果字符串是十进制字符串(0~9),则返回True,否则返回False。
		""" str_var1 = "134577309285" print(str_var1.isdecimal()) """
 |      A string is a decimal string if all characters in the string are decimal and
 |      there is at least one character in the string.
		# 如果字符串中的所有字符都是十进制字符,并且该字符串为十进制字符串字符串中至少有一个字符。
 |  
isdigit(self, /)
 |      Return True if the string is a digit string, False otherwise.
 |      # 如果字符串是数字字符串,则返回True,否则返回False。
		""" str_var1 = "134577309285" print(str_var1.isdecimal()) """
 |      A string is a digit string if all characters in the string are digits and there
 |      is at least one character in the string.
 |  	# 如果字符串中的所有字符都是数字,并且字符串中至少有一个字符。
isidentifier(self, /)
 |      Return True if the string is a valid Python identifier, False otherwise.
 |      # 如果字符串是有效的Python标识符,则返回True,否则返回False。
		""" str_var1 = "for" print(str_var1.isidentifier()) """
 |      Call keyword.iskeyword(s) to test whether string s is a reserved identifier,
    	# Call关键字。iskeyword测试字符串s是否为保留标识符,
 |      such as "def" or "class".
 |  	# 例如“def”或“class”。
islower(self, /)
 |      Return True if the string is a lowercase string, False otherwise.
 |      # 如果字母字符串是小写字符串,则返回True,否则返回False。
		""" str_var1 = "for" str_var2 = "For" str_var3 = "123" print(str_var1.islower()) print(str_var2.islower()) print(str_var3.islower()) """
 |      A string is lowercase if all cased characters in the string are lowercase and
 |      there is at least one cased character in the string.
 |  	# 如果字符串中的所有字母字符都是小写的,则字符串是小写的,并且字符串中至少有一个字母字符。
isnumeric(self, /)
 |      Return True if the string is a numeric string, False otherwise.
 |      # 如果字符串是数字字符串,则返回True,否则返回False。
    	""" str_var1 = "for" str_var2 = "For" str_var3 = "123" print(str_var1.isnumeric()) print(str_var2.isnumeric()) print(str_var3.isnumeric()) """
 |      A string is numeric if all characters in the string are numeric and there is at
 |      least one character in the string.
    	# 如果字符串中的所有字符都是数字,并且字符串中至少有一个字符。
        
isprintable(self, /)
 |      Return True if the string is printable, False otherwise.
 |      # 如果字符串可打印,则返回True,否则返回False。
		# 非打印字符是指在 Unicode 字符数据库中定义为“其他”或“分隔符”的字符,但 ASCII 空格(0x20)除外,它被认为是可打印的。(请注意,此上下文中的可打印字符是在对字符串调用 repr( ) 时不应转义的字符。它与写入的字符串的处理无关系统标准输出或者系统标准。)
		""" str_var3 = "123" print(str_var1.isprintable()) c = 'dq\tddq\nn' print(c) print(c.isprintable()) """
 |      A string is printable if all of its characters are considered printable in
 |      repr() or if it is empty.
 |  	# 如果字符串的所有字符都可以在中打印,则该字符串是可打印的repr()或如果为空。
isspace(self, /)
 |      Return True if the string is a whitespace string, False otherwise.
 |      # 如果字符串是空白字符串,则返回True,否则返回False。
    	""" str_var1 = " " str_var2 = "For" print(str_var1.isspace()) print(str_var2.isspace()) """
 |      A string is whitespace if all characters in the string are whitespace and there
 |      is at least one character in the string.
 |  	# 如果字符串中的所有字符都是空白,并且字符串中至少有一个字符。
istitle(self, /)
 |      Return True if the string is a title-cased string, False otherwise.
 |      # 如果字符串是以标题为大小写的字符串,则返回True,否则返回False。
		""" str_var1 = "Hello Word!" print(str_var1.istitle()) """
 |      In a title-cased string, upper- and title-case characters may only
 |      follow uncased characters and lowercase characters only cased ones.
 |  	# 在标题大小写字符串中,大写和标题大小写字符只能遵循无大小写字符和小写字符,仅遵循大小写字符。
isupper(self, /)
 |      Return True if the string is an uppercase string, False otherwise.
 |      # 如果字符串为大写字符串,则返回True,否则返回False。
		""" str_var1 = "Hello" str_var2 = "HELLO" print(str_var1.isupper()) print(str_var2.isupper()) """
 |      A string is uppercase if all cased characters in the string are uppercase and
 |      there is at least one cased character in the string.
 |  	# 如果字符串中所有大小写字符均为大写,则字符串为大写,并且字符串中至少有一个大小写字符。
join(self, iterable, /)
 |      Concatenate any number of strings.
 |      # 串联任意数量的字符串。
		""" list_var = ['qw', 'wer', 'asd'] str_var1 = "Hello" str_var2 = str_var1.join(list_var) print(str_var2) """
 |      The string whose method is called is inserted in between each given string.
    	# 调用其方法的字符串插入到每个给定字符串之间。
 |      The result is returned as a new string.
 |      # 结果将作为新字符串返回。
 |      Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
 |  	#示例:“.”。加入(['ab',pq',rs'])->'ab.pq。卢比'
ljust(self, width, fillchar=' ', /)
 |      Return a left-justified string of length width.
 |      # 返回长度宽度为左对齐的字符串。
		""" str_var1 = "Hello" print(str_var1.ljust(10, '*')) """
 |      Padding is done using the specified fill character (default is a space).
    	# 填充是使用指定的填充字符完成的(默认为空格)。
 |  
lower(self, /)
 |      Return a copy of the string converted to lowercase.
 |  	# 返回转换为小写的字符串的副本。
    	""" str_var1 = "Hello" print(str_var1.lower()) """
lstrip(self, chars=None, /)
 |      Return a copy of the string with leading whitespace removed.
 |      # 返回删除前导空格的字符串副本。
		""" str_var1 = "***Hello" print(str_var1.lstrip("*")) """
 |      If chars is given and not None, remove characters in chars instead.
 |  	# 如果给定了chars而不是None,请删除chars中的字符。
partition(self, sep, /)
 |      Partition the string into three parts using the given separator.
 |      # 使用给定的分隔符将字符串分为三部分。
		""" str_var1 = "7+8" print(str_var1.partition("+")) """
 |      This will search for the separator in the string.  If the separator is found,
 |      returns a 3-tuple containing the part before the separator, the separator
 |      itself, and the part after it.
 |      # 这将在字符串中搜索分隔符。如果找到分隔符,返回一个3元组,其中包含分隔符之前的部分,分隔符它本身和它后面的部分。
 |      If the separator is not found, returns a 3-tuple containing the original string
 |      and two empty strings.
 |   	# 如果未找到分隔符,则返回包含原始字符串的3元组,和两个空字符串。
replace(self, old, new, count=-1, /)
 |      Return a copy with all occurrences of substring old replaced by new.
 |      # 返回一个副本,其中所有出现的子字符串旧替换为新。
		""" str_var1 = "7+8" print(str_var1.replace("+", "/")) """
 |        count
 |          Maximum number of occurrences to replace.
			# 要替换的最大出现次数。
 |          -1 (the default value) means replace all occurrences.
 |      	# -1(默认值)表示替换所有引用。
 |      If the optional argument count is given, only the first count occurrences are
 |      replaced.
 |  	# 如果给定了可选的参数计数,则只会出现第一次计数已替换
rfind(...)
 |      S.rfind(sub[, start[, end]]) -> int
 |      
 |      Return the highest index in S where substring sub is found,
		# 返回在S中找到子字符串sub的最高索引,
		""" str_var1 = "abcabcbac" print(str_var1.rfind("abc")) # 3 """

 |      such that sub is contained within S[start:end].  Optional
   		# 使sub包含在S[开始:结束]中。可选择的
 |      arguments start and end are interpreted as in slice notation.
 |      # 参数start和end被解释为切片表示法。
 |      Return -1 on failure.
 |  	# 故障时返回-1。
rindex(...)
 |      S.rindex(sub[, start[, end]]) -> int
 |      
 |      Return the highest index in S where substring sub is found,
    	# 返回在S中找到子字符串sub的最高索引,
		""" str_var1 = "abcabcbac" print(str_var1.rindex("abc")) # 3 """
 |      such that sub is contained within S[start:end].  Optional
      	# 使sub包含在S[开始:结束]中。可选择的
 |      arguments start and end are interpreted as in slice notation.
 |      # 参数start和end被解释为切片表示法。
 |      Raises ValueError when the substring is not found.
 |  	# 找不到子字符串时引发ValueError。
rjust(self, width, fillchar=' ', /)
 |      Return a right-justified string of length width.
 |      # 返回长度-宽度为右对齐的字符串。
		""" str_var1 = "Hello" print(str_var1.rjust(10, '*')) """
 |      Padding is done using the specified fill character (default is a space).
 |  	# 填充是使用指定的填充字符完成的(默认为空格)。
rpartition(self, sep, /)
 |      Partition the string into three parts using the given separator.
 |      # 使用给定的分隔符将字符串分为三部分。
		""" str_var1 = "7+8" print(str_var1.rpartition("+")) """
 |      This will search for the separator in the string, starting at the end. If
 |      the separator is found, returns a 3-tuple containing the part before the
 |      separator, the separator itself, and the part after it.
 |      # 这将在字符串中搜索分隔符,从末尾开始。如果如果找到分隔符,则返回一个3元组,其中包含分隔符之前、分隔符本身及其后面的部分。
 |      If the separator is not found, returns a 3-tuple containing two empty strings
 |      and the original string.
 |  	# 如果未找到分隔符,则返回包含两个空字符串的3元组和原始字符串。
rsplit(self, /, sep=None, maxsplit=-1)
 |      Return a list of the words in the string, using sep as the delimiter string.
 |      # 使用sep作为分隔符字符串,返回字符串中的单词列表(从结尾开始计数)。
		""" str_var1 = "7+8" print(str_var1.split(sep="+")) """
 |        sep
 |          The delimiter according which to split the string.
			# 分割字符串所依据的分隔符。
 |          None (the default value) means split according to any whitespace,
			# None(默认值)表示根据任何空格进行拆分,
 |          and discard empty strings from the result.
			# 并从结果中丢弃空字符串。
 |        maxsplit
 |          Maximum number of splits to do.
    		# 要执行的最大拆分数。
 |          -1 (the default value) means no limit.
 |      	# -1(默认值)表示没有限制。
 |      Splits are done starting at the end of the string and working to the front.
 |  		# 拆分从字符串的末尾开始,一直到前面。
rstrip(self, chars=None, /)
 |      Return a copy of the string with trailing whitespace removed.
 |      # 返回删除尾部空格的字符串副本。
		""" str_var1 = "7+8*****" print(str_var1.rstrip("*")) """
 |      If chars is given and not None, remove characters in chars instead.
 |  	# 如果给定了chars而不是None,请删除chars中的字符。
split(self, /, sep=None, maxsplit=-1)
 |      Return a list of the words in the string, using sep as the delimiter string.
 |      # 使用sep作为分隔符字符串,返回字符串中的单词列表。
 |      sep
 |        The delimiter according which to split the string.
 |        None (the default value) means split according to any whitespace,
 |        and discard empty strings from the result.
		# 分割字符串所依据的分隔符。
		# 并从结果中丢弃空字符串。
        # None(默认值)表示根据任何空格进行拆分,
 |      maxsplit
 |        Maximum number of splits to do.
 |        -1 (the default value) means no limit.
 |  	# 要执行的最大拆分数。
    	# -1(默认值)表示没有限制。
splitlines(self, /, keepends=False)
 |      Return a list of the lines in the string, breaking at line boundaries.
 |      # 返回字符串中的行列表,在行边界处打断。
		""" str_var1 = "7+8*****\nqweqwedas" print(str_var1.splitlines())# ['7+8*****', 'qweqwedas'] """
 |      Line breaks are not included in the resulting list unless keepends is given and
 |      true.
 |  	#结果列表中不包括换行符,除非给出了keepends,并且为真
startswith(...)
 |      S.startswith(prefix[, start[, end]]) -> bool
 |      
 |      Return True if S starts with the specified prefix, False otherwise.
 |      With optional start, test S beginning at that position.
 |      With optional end, stop comparing S at that position.
 |      prefix can also be a tuple of strings to try.
		""" 如果S以指定前缀开头,则返回True,否则返回False。 使用可选启动,测试S从该位置开始。 使用可选结束,停止在该位置比较S。 前缀也可以是要尝试的字符串元组。 """
 |  
strip(self, chars=None, /)
 |      Return a copy of the string with leading and trailing whitespace removed.
 |      # 返回删除前导和尾随空格的字符串副本。

 |      If chars is given and not None, remove characters in chars instead.
 |  	# 如果给定了chars而不是None,请删除chars中的字符。
swapcase(self, /)
 |      Convert uppercase characters to lowercase and lowercase characters to uppercase.
 |  	# 将大写字符转换为小写,将小写字符转换为大写。
        """ str_var1 = "HEllo" print(str_var1.swapcase()) """
title(self, /)
 |      Return a version of the string where each word is titlecased.
 |      # 返回每个单词标题所在的字符串版本。
        """ str_var1 = "hello word" print(str_var1.title()) """
 |      More specifically, words start with uppercased characters and all remaining
 |      cased characters have lower case.
 |  	# 更具体地说,单词以大写字符开头,其余所有字符大小写字符使用小写。
translate(self, table, /)
 |      Replace each character in the string using the given translation table.
 |      # 使用给定的翻译表替换字符串中的每个字符。
		""" s = 'ABCDBCA' print(s.translate({ord('A'): ord('a'), ord('B'): ord('b'), ord('C'): None})) print(s.translate({ord('A'): 'X', ord('B'): 'YZ', ord('C'): None})) """
 |        table
 |          Translation table, which must be a mapping of Unicode ordinals to
 |          Unicode ordinals, strings, or None.
 |      	# 转换表,它必须是Unicode序号到的映射,Unicode序号、字符串或无。
 |      The table must implement lookup/indexing via __getitem__, for instance a
 |      dictionary or list.  If this operation raises LookupError, the character is
 |      left untouched.  Characters mapped to None are deleted.
 |  	# 该表必须通过\uu getitem\uuuuu实现查找/索引,例如字典或列表。如果此操作引发LookupError,则字符为保持不变。将删除映射到“无”的字符。
 |  upper(self, /)
 |      Return a copy of the string converted to uppercase.
    	# 返回转换为大写的字符串的副本。
 |  
zfill(self, width, /)
 |      Pad a numeric string with zeros on the left, to fill a field of the given width.
 |      # 在左侧用零填充数字字符串,以填充给定宽度的字段。
        """ str_var = "123" print(str_var.zfill(10)) """
 |      The string is never truncated.
    	# 字符串永远不会被截断。

4、格式化输出:

姓名       年龄        性别        家庭住址
xxx       xxx
list_data = [{name: xxx, age: xxx, gender: xxx, address}, .....]
包含居中对齐,向左对齐, 向右对齐
print("{:^5}\t{:^5}\t{:^5}\t{:^30}".format("姓名", "年龄", "性别", "家庭住址"))
list_data = [{
    "name": "王海林", "age": 19, "gender": "男", "address": "陕西省汉中市宁强县"},
             {
    "name": "王海林", "age": 19, "gender": "男", "address": "陕西省汉中市宁强县"},
             {
    "name": "王海林", "age": 19, "gender": "男", "address": "陕西省汉中市宁强县"}]
print("{name:^5}\t{age:^5}\t{gender:^5}\t{address:^30}".format_map(list_data[0]))
print("{name:<5}\t{age:<5}\t{gender:<5}\t{address:<30}".format_map(list_data[1]))
print("{name:>5}\t{age:>5}\t{gender:>5}\t{address:>30}".format_map(list_data[2]))
原网站

版权声明
本文为[我是渣渣辉]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_63342921/article/details/125427673