Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

How to add whitespaces between two words in a string so that total string length has specific value

I am starting out with strings (creditcard numbers and validity status) such as:

'378282246310005 Invalid',
'30569309025904 Invalid',
'6011111111111117 valid'

and I would like to obtain final strings, where the total length is 40:

'378282246310005                  Invalid',
'30569309025904                   Invalid',
'6011111111111117                   valid'

Besides the Python string methods 'rjust', 'ljust' and 'center', are there any built-in string method to accomplish such a task, or would I need to write some function for it for example?

So far, I have tried:

string = '378282246310005 {} Invalid'
while len(string) < 40:
    string = string.format(' ')

Not sure how to progress from here.

Upvotes: 3

Views: 87

Answers (4)

rushabh
rushabh

Reputation: 1

I tried below code and its working Lets say we need total length of string after adding space in between is 12.

    String str1 = "Rohit", str2 = "BHA";
    String finalString = str1;
    for(int i = 1 ; i <= 12 - str1.length() - str2.length() ; i++)
    {
        finalString += " ";
    }
    finalString += str2;
    System.out.println("iban curr : "+finalString);

Now finalString have value : "Rohit BHA" (4 space in between)

Upvotes: -2

hygull
hygull

Reputation: 8740

The following approach will also be very helpful for you.

>>> p = 353535353535
>>>
>>> credit_card_no = 378282246310005
>>> status = "Invalid"
>>>
>>> s = str(credit_card_no)
>>> l = len(s)
>>>
>>> format_string = "{:<" + str(40 - len(status)) + "}" + status
>>>
>>> answer = format_string.format(s)
>>> answer
'378282246310005                  Invalid'
>>>
>>> len(answer)
40
>>>

Finally, based on the above approach, we can write a reusable function like below.

def get_formatted_string(credit_card_no, status, width=40):
    s = str(credit_card_no)
    l = len(s)

    format_string = "{:<" + str(width - len(status)) + "}" + status
    answer = format_string.format(s)

    return answer

if __name__ == "__main__":
    inp1 = (378282246310005, 'Invalid')
    inp2 = (30569309025904, 'Invalid')
    inp3 = (6011111111111117, 'valid')

    print(get_formatted_string(*inp1))
    print(get_formatted_string(*inp2))
    print(get_formatted_string(*inp3))


# E:\Users\Rishikesh\Projects\Python3\try>python Stk_format_str.py
# 378282246310005                  Invalid
# 30569309025904                   Invalid
# 6011111111111117                   valid
References »

Upvotes: 1

TerryA
TerryA

Reputation: 59974

You can also use Python 3's string formatting:

>>> a = '378282246310005 Invalid'
>>> L = a.split()
>>> middle = 40 - len(L[0])
>>> print(L[0] + f'{L[1]: >{middle}}')
378282246310005                  Invalid

Dealing with a list:

>>> for item in L:
...     item_split = item.split()
...     middle = 40 - len(item_split[0])
...     print(item_split[0] + f'{item_split[1]: >{middle}}')
... 
378282246310005                  Invalid
30569309025904                   Invalid
6011111111111117                   valid

Upvotes: 1

jpp
jpp

Reputation: 164643

Here's a manual solution using str.split and str.join:

L = ['378282246310005 Invalid',
     '30569309025904 Invalid',
     '6011111111111117 valid']

def formatter(x):
    x_split = x.split()
    n = sum(map(len, x_split))
    return (' '*(40-n)).join(x_split)

print(*map(formatter, L), sep='\n')

378282246310005                  Invalid
30569309025904                   Invalid
6011111111111117                   valid

This works even if your input string contains multiple whitespace.

Upvotes: 2

Related Questions