Given a width n
, and an index i
, how to generate a string of length n
with x
at index i
, and .
at the remaining indices?
For example:
Input: n = 4, i = 1
Output: ".x.."
Currently I'm doing:
"".join("x" if j == i else "." for j in range(n))
Another option:
("." * i) + "x" + ("." * (n - i - 1))
I can also do:
f"{('.' * i)}x{('.' * (n - i - 1))}"
All work, but I'm wondering if there's a way to do this with f-string, perhaps using some form of padding, as shown here?
You can use a mix of ljust and must multiplying to create a string
>>> f"{'.' * i}x".ljust(n, ".")
'.x..'
Or just using f strings alone by specifying a separator for the alignment
f"{'.' * i}{'x':.<{n-i}}"