简体   繁体   中英

How to automatically add the character _ into a string or list which is input by a user to make 2 lists the same length

string1 = str(input("STRING 1: "))
string2 = str(input("STRING 2: "))


difference = 0

print(string1)
print(string2)


for i in string2:
    if len(string1) < len(string2):
        difference = len(string2) - len(string1)
        difference = difference*"-"
        string2 = string2.append(difference)
for i in string1:        
    if len(string2) < len(string1):
        difference = len(string1) - len(string2)
        difference = difference*"-"
        string1 = string1.append(difference)

        
print(string1)
print(string2)

#for loop to find longest string #and what the difference in string length #apend the character "-"to the end of the shoter string to make the 2 strings the same length # eg. johnjones # mary-----

No need to use for.

append is the function of list.

You should declare a list.

Or you use +=.

string1 = str(input("STRING 1: "))
string2 = str(input("STRING 2: "))

difference = 0

result_list = list()

if len(string1) < len(string2):
    difference = len(string2) - len(string1)
    difference = difference * "-"
    string1 += difference
    result_list.append(string2 + difference)
    
if len(string2) < len(string1):
    difference = len(string1) - len(string2)
    difference = difference * "-"
    string2 += difference
    result_list.append(string2 + difference)
    

print(result_list)
print(string1)
print(string2)

This is one of the methods:

string1 = str(input("STRING 1: "))
string2 = str(input("STRING 2: "))

difference = 0

print(string1)
print(string2)

if len(string1) < len(string2):
    difference = len(string2) - len(string1)
    for i in range(difference):
        string1 +="-"
elif len(string2) < len(string1):
    difference = len(string1) - len(string2)
    for i in range(difference):
        string2 +="-"
    
print(string1)
print(string2)
string1 = str(input("STRING 1: "))
string2 = str(input("STRING 2: "))

difference = 0

print(string1)
print(string2)

for i in string2:
    if len(string1) < len(string2):
        difference = len(string2) - len(string1)
        difference = difference * "-"
        string1 = string1 + difference
for i in string1:
    if len(string2) < len(string1):
        difference = len(string1) - len(string2)
        difference = difference * "-"
        string2 = string2 + difference

print(string1)
print(string2)

Using string-formatting and the max-function we can dynamically "pad" the shorter string:

string1 = "johnjones"
string2 = "mary"
strList = [string1, string2]
print("{:-<{}}".format(string1, len(max(strList, key=lambda x: len(x)))))
print("{:-<{}}".format(string2, len(max(strList, key=lambda x: len(x)))))
#Output: johnjones
#Output: mary-----

The variables string1 and string2 are not changed using the above code, just their representation in the output.

You can read more about the syntax of string-formatting here .

However, this is a short explainer of what the different parts mean in the above code:

  • {} is a replacement-field , this is the location that the str.format method tries to input it's variables into.
  • : placed inside of a replacement-field is called a format-specifier which tells str.format how to handle the variable.
  • - in the expression {:-<{}} is the fill -character that should be used in the formatting.
  • < is the text-alignment of the string. < means that the text is anchored to the left, > is anchored to the right, and ^ centers the text.
  • {} inside of the expression {:-<{}} is just another replacement-field that will be populated by str.format , in this case however, it refers to width of the variable.

I don't know why are referring to list, but, from what I can understand, you could just do something like this:

delta = len(str1) - len(str2)
if delta < 0:
    str1 += '_'*abs(delta)
else:
    str2 += '_'*abs(delta)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM