I want to copy whitespace like spaces and tabs from string to string in Python 2. For example if I have a string with 3 spaces at first and one tab at the end like " Hi\t" I want to copy those whitespace to another string for example string like "hello" would become " hello\t" Is that possible to do easily?
1 Answer
Yes, this is of course possible. I would use regex for that.
import re
hi = " Hi\t"
hello = "hello"
spaces = re.match(r"^(\s*).+?(\s*)$", hi)
if spaces:
left, right = spaces.groups()
string = "{}{}{}".format(left, hello, right)
print(string)
# Out: " hello\t"