python字符串使用
- 格式化
参考,python3.7.5文档
class string.Formatter¶
The Formatter class has the following public methods:
format(format_string, *args, **kwargs)
The primary API method. It takes a format string and an arbitrary set of positional and keyword arguments. It is just a wrapper that calls vformat().(这个函数调用vformat)
Changed in version 3.7: A format string argument is now positional-only.
vformat(format_string, args, kwargs)
This function does the actual work of formatting. It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the *args and **kwargs syntax. vformat() does the work of breaking up the format string into character data and replacement fields. It calls the various methods described below.
a,b,c,d=1,2,3,4
print("{1}{2}{3}{0}".format(a,b,c,d))
输出: 2341
字符串long是否以short开头
def compare_two_string_opening(long,short):
for position in range(len(short)):
if long[position]!=short[position]:
return False
return True
def compare_two_string_opening_v1(long,short):
lenth=len(short)
begining=long[0:lenth]
if begining==short:
return True
else
return False
def compare_two_string_opening_v2(long,short):
return long[0:len(short)]==short