You can do this with str.ljust(width[, fillchar]):
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than
len(s).
>>> 'hi'.ljust(10)
'hi '
Answer from Felix Kling on Stack OverflowYou can do this with str.ljust(width[, fillchar]):
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than
len(s).
>>> 'hi'.ljust(10)
'hi '
For a flexible method that works even when formatting complicated string, you probably should use the string-formatting mini-language,
using either f-strings
>>> f'{"Hi": <16} StackOverflow!' # Python >= 3.6
'Hi StackOverflow!'
or the str.format() method
>>> '{0: <16} StackOverflow!'.format('Hi') # Python >=2.6
'Hi StackOverflow!'
STRING METHOD FOR SPACES
python - Efficient way to add spaces between characters in a string - Stack Overflow
python - Add a space in string - Stack Overflow
\n giving me an extra space on new line?
Is there any string method that checks for spaces? I know isspace() checks for them but it only gives a true if the string only includes white spaces. I'm looking for one that will prince a false if there is any spaces. I'm using these for a project that splices emails so isalpha() wont work cos of that @ and numeric symbols
here is the code
print("Your email may not contain any spaces.")
email = input("Enter your email: ")
index = email.index("@") # this method will return a number
print(index)
username = email[:index]
domain = email[index:]
print(f"your username is {username} and domain is {domain}")
s = "BINGO"
print(" ".join(s))
Should do it.
s = "BINGO"
print(s.replace("", " ")[1: -1])
Timings below
$ python -m timeit -s's = "BINGO"' 's.replace(""," ")[1:-1]'
1000000 loops, best of 3: 0.584 usec per loop
$ python -m timeit -s's = "BINGO"' '" ".join(s)'
100000 loops, best of 3: 1.54 usec per loop
print "How many times did " + name + " go here?"
or
print "How many times did", name, "go here?"
or
print "How many times did %s go here?" % name
The preferred form in this simple case is the second one. The first uses concatenation (which is useful if you want more or less than one space between parts), the second uses the comma operator, which in the context of print joins the strings with a space, and the third uses string formatting (the old style), which should look familiar if you come from C, Perl, PHP, etc. The third is the most powerful form, but in this simple cases the use of format strings is unnecessary.
Note that in Python, lines do not need to be (and should not be) ended by semicolons. You can also use some of the string justification methods to add several spaces on either or both sides of the string.
@yookd: Welcome to SO. This is not a real answer, just some suggestions for asking better questions.
Please check what you have typed before posting. Your print statement does not print what you say it does. In fact it doesn't print anything, because it has a syntax error.
>>> name = "Foo"
>>> print "How many times did " + name "go here?";
File "<stdin>", line 1
print "How many times did " + name "go here?";
^
SyntaxError: invalid syntax
You are missing a + after name:
>>> print "How many times did " + name + "go here?";
How many times did Foogo here?
And even after fixing the syntax error it doesn't do what you said it does. What it does do is demonstrate one of the ways of getting a space (including it in the constant text).
Hint: To save checking, enter your code at the Python interactive prompt and then copy/paste the code and the results straight into your question, just as I did in this "answer".