【发布时间】:2021-02-18 12:21:29
【问题描述】:
如何获取字符串的长度以包含表示字典中键的变量的长度?
代码
def string_length(mystring):
""" I am the doc string for this function"""
length_of_string = len(mystring)
print("mystring : ",mystring)
print("length of string : ", length_of_string)
print('\n'*2)
return length_of_string
def heading_from_string(mystring, symbol_string):
los = string_length(mystring)
print(symbol_string * los, '\n', mystring, '\n', symbol_string * los, sep = '')
a = {"imagefile1":2, "Image_file_2":3}
for k,v in a.items():
print('\n'*2)
mystring = ("testing_testing :", k)
heading_from_string(mystring, "&")
返回
mystring : ('testing_testing :', 'imagefile1')
length of string : 2
&&
('testing_testing :', 'imagefile1')
&&
mystring : ('testing_testing :', 'Image_file_2')
length of string : 2
&&
('testing_testing :', 'Image_file_2')
&&
预期输出 符号的数量应该与mastering中的文本长度和dict key k中的长度相同 例如
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
testing_testing :', 'imagefile1'
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
【问题讨论】:
标签: python string dictionary string-length