Given a starter string, and a number of spaces needing to be added to a string, is there an easy way to do this? If the number of spaces is uneven spread, add spaces left to right.
This is what I've tried:
dividing space number with actual string spaces
div = spaces // (word_count - 1)
tokenize string
temp = st.split()
for i in range(len(temp)):
If first word just add the current word w/o spaces
if i == 0:
st = temp[0]
If first word just add the current word w/o spaces
else:
add div amount of spaces + original space to word
st = st + " "*div + " " +temp[i]
update our space count
space_count = space_count - div
space_count matches or is smaller than actual amount of spaces in string
if space_count <= word_count -1:
st = st.replace(" ", " ", space_count)
add an extra space, 'space_count' amount of times. This is where the problem is, it only replaces the first space_count amount of white spaces characters. Any tips on how to add the last spaces, or finding a better way to do this?
Here is an example:
"Algernon. Did you hear what I was playing, Lane?"
and given space_Count = 12, should give
Algernon. Did you hear what I was playing, Lane?
Edit: got it working!
Using re. sub() or split() + join() method to remove extra spaces between words in Python.
Use the str. ljust() method to add spaces to the end of a string, e.g. result = my_str. ljust(6, ' ') . The ljust method takes the total width of the string and a fill character and pads the end of the string to the specified width with the provided fill character.
I think you're on the right track here, but you seem to be having trouble replacing the rightmost instances rather than the leftmost instances. To replace starting from the right, rather than from the left, you can reverse your string, then do the replace
operation, and then reverse it again. In the following snippet I do this by string slicing: sentence[::-1]
.
So the following snippet first calculates the number of spaces in the original phrase (to be replaced) (num_spaces
), then the floor of the number of spaces that need to be added per space in the original (div
), and then the number of extra spaces that need to be added from the right (num_extra
). Afterwards, it reverses the string and replaces num_extra
spaces with ' ' + ' ' * div
; that's the original space, plus div
more spaces, plus one extra space as a remainder. Then the string is reversed again and the rest of the spaces are replaced with just ' ' + ' ' * div
- the same thing but without the remainder space.
def add_spaces(sentence, ct):
num_spaces = sentence.count(' ') # find number of spaces in the original phrase
div = (ct // num_spaces) # find base of number of spaces to add
num_extra = ct % num_spaces # find the number of left-over spaces
# now use string replacements to re-add the spaces.
# first, change the rightmost num_extra occurrences to have more spaces,
# by reversing the string, doing the replace, and then un-reversing it.
# this is somewhat of a hacky work-around for replacing starting from right
new_sentence = sentence[::-1].replace(' ', ' ' + ' ' * div, num_extra)[::-1]
# then, simply replace the rest of the spaces properly
new_sentence = new_sentence.replace(' ', ' ' + ' ' * div, num_spaces - num_extra)
return new_sentence
When I try it in my console, this snippet spits out:
>>> add_spaces("Algernon. Did you hear what I was playing, Lane?", 12)
'Algernon. Did you hear what I was playing, Lane?'
Of course, putting the remainder spaces in from the left instead of from the right would return the string you mention in your question. That modification should be straightforward.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With