UPDATED ANSWER:
You can compose multiple format specifiers. Below, +.1f is used to keep the sign, and print to only 1 decimal place. and <10 is used to justify the string.
values = [-1.0, 2.2]
for value in values:
print(f"someLjustedStrings{value:<+10.1f}someLjustedStrings")
OLD ANSWER:
You can try this:
values = [-1.0, 2.2]
for value in values:
print(f"someLjustedStrings{['+', '-'][value<0]}{abs(value):<10}someLjustedStrings")
Output:
someLjustedStrings-1.0 someLjustedStrings
someLjustedStrings+2.2 someLjustedStrings
< followed by int for ljust
> followed by int for rjust
EDIT:
If you want to ljust by variable:
LJUST = {'value': 10}
values = [-1.0, 2.2]
for value in values:
print(f"someLjustedStrings{['+', '-'][value<0]}{abs(value):<{LJUST['value']}}someLjustedStrings")
Output:
someLjustedStrings-1.0 someLjustedStrings
someLjustedStrings+2.2 someLjustedStrings
Answer from Sayandip Dutta on Stack OverflowW3Schools
w3schools.com › python › ref_string_ljust.asp
Python String ljust() Method
Note: In the result, there are actually 14 whitespaces to the right of the word banana. The ljust() method will left align the string, using a specified character (space is default) as the fill character.
Programiz
programiz.com › python-programming › methods › string › ljust
Python String ljust()
The string ljust() method returns a left-justified string of a given minimum width.
ljust and rjust in Python
06:41
ljust() Function | rjust() Function | built in functions in Python ...
17:08
29. String Alignment using f-string and ljust(), rjust(), center() ...
03:39
Left Justify And Right Justify A String With ljust() rjust() | ...
02:35
Python ljust() string method - YouTube
Tutorialspoint
tutorialspoint.com › python › string_ljust.htm
Python String ljust() Method
The Python String ljust() method is used to left align the string with the width specified. If the specified width is more than the length of the string, then the remaining part of the string is filled with fillchar.
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › ljust.html
ljust — Python Reference (The Right Way) 0.1 documentation
ljust · Edit on GitHub · Returns the string left justified in a string of specified length. str. ljust(width[, fillchar]) width · Required. The width of the field containing the string. fillchar · Optional. Specifies the padding character (default is a space).
GeeksforGeeks
geeksforgeeks.org › python › python-string-ljust-rjust-center
Python String - ljust(), rjust(), center() - GeeksforGeeks
January 2, 2025 - The return type of ljust() is a new string that is left-justified with padding.
Codecademy
codecademy.com › docs › python › strings › .ljust()
Python | Strings | .ljust() | Codecademy
November 26, 2023 - Left-aligns a string with a specified fill character
Javatpoint
javatpoint.com › python-string-ljust-method
Python String | ljust() method with Examples - Javatpoint
Python Function Programs · capitalize() casefold() center(width ,fillchar) count(string,begin,end) encode() endswith(suffix ,begin=0,end=len(string)) expandtabs(tabsize = 8) find(substring ,beginIndex, endIndex) format(value) index(subsring, beginIndex, endIndex) isalnum() isalpha() isdecimal() isdigit() isidentifier() islower() isnumeric() isprintable() isupper() isspace() istitle() isupper() join(seq) ljust(width[,fillchar]) lower() lstrip() partition() replace(old,new[,count]) rfind(str,beg=0,end=len(str)) rindex(str,beg=0,end=len(str)) rjust(width,[,fillchar]) rstrip() rsplit(sep=None, ma
Python Principles
pythonprinciples.com › blog › string-ljust-explained
Python String ljust() explained – Python Principles
The method lets you pad a string with characters up to a certain length. The ljust method means "left-justify"; this makes sense because your string will be to the left after adjustment up to the specified length.
TutorialKart
tutorialkart.com › python › python-string-ljust
Python String ljust() - Examples
April 3, 2023 - Python String ljust() is used to left align the given string in specified space (length) and optionally fill the left out space with the specified character.
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.ljust.html
pandas.Series.str.ljust — pandas 3.0.5 documentation - PyData |
Pad right side of strings in the Series/Index. Equivalent to str.ljust().