Is it possible to trim it from the beginning with % formatting?
Python's % formatting comes from C's printf.
Note that the . indicates precision for a float. That it works on a string is a mere side effect, and unfortunately, there is no provision in the string formatting specification to accommodate stripping a string from the left to a fixed max width.
Therefore if you must strip a string to a fixed width from the end, I recommend to slice from a negative index. This operation is robust, and won't fail if the string is less than 10 chars.
>>> up_to_last_10_slice = slice(-10, None)
>>> 'Lorem Ipsum'[up_to_last_10_slice]
'orem Ipsum'
>>> 'Ipsum'[up_to_last_10_slice]
'Ipsum'
str.format also no help
str.format is of no help here, the width is a minimum width:
>>> '{lorem:>10}'.format(lorem='Lorem Ipsum')
'Lorem Ipsum'
>>> '{lorem:*>10}'.format(lorem='Lorem')
'*****Lorem'
(The asterisk, "*", is the fill character.)
Is it possible to trim it from the beginning with % formatting?
Python's % formatting comes from C's printf.
Note that the . indicates precision for a float. That it works on a string is a mere side effect, and unfortunately, there is no provision in the string formatting specification to accommodate stripping a string from the left to a fixed max width.
Therefore if you must strip a string to a fixed width from the end, I recommend to slice from a negative index. This operation is robust, and won't fail if the string is less than 10 chars.
>>> up_to_last_10_slice = slice(-10, None)
>>> 'Lorem Ipsum'[up_to_last_10_slice]
'orem Ipsum'
>>> 'Ipsum'[up_to_last_10_slice]
'Ipsum'
str.format also no help
str.format is of no help here, the width is a minimum width:
>>> '{lorem:>10}'.format(lorem='Lorem Ipsum')
'Lorem Ipsum'
>>> '{lorem:*>10}'.format(lorem='Lorem')
'*****Lorem'
(The asterisk, "*", is the fill character.)
This can easily be done through slicing, so you do not require any string format manipulation to do your JOB
>>> "Lorem Ipsum"[-10:]
'orem Ipsum'
Enhanced String Formatting for Truncation with Ellipses - Ideas - Discussions on Python.org
Limiting Python input strings to certain characters and lengths - Stack Overflow
How to limit string length?
Python truncate a long string - Stack Overflow
Question 1: Restrict to certain characters
You are right, this is easy to solve with regular expressions:
import re
input_str = raw_input("Please provide some info: ")
if not re.match("^[a-z]*$", input_str):
print "Error! Only letters a-z allowed!"
sys.exit()
Question 2: Restrict to certain length
As Tim mentioned correctly, you can do this by adapting the regular expression in the first example to only allow a certain number of letters. You can also manually check the length like this:
input_str = raw_input("Please provide some info: ")
if len(input_str) > 15:
print "Error! Only 15 characters allowed!"
sys.exit()
Or both in one:
import re
input_str = raw_input("Please provide some info: ")
if not re.match("^[a-z]*$", input_str):
print "Error! Only letters a-z allowed!"
sys.exit()
elif len(input_str) > 15:
print "Error! Only 15 characters allowed!"
sys.exit()
print "Your input was:", input_str
Regexes can also limit the number of characters.
r = re.compile("^[a-z]{1,15}$")
gives you a regex that only matches if the input is entirely lowercase ASCII letters and 1 to 15 characters long.
info = (data[:75] + '..') if len(data) > 75 else data
This code matches the JavaScript, but you should consider using data[:73] so that the total result including the .. fits in 75 characters.
Even more concise:
data = data[:75]
If it is less than 75 characters there will be no change.
I am coding a simple hangman game, and I was to make the user's input a maximum of 1 character. How do I make the maximum amount of letters that will be accepted, 1?
Python's input function cannot do this directly; but you can truncate the returned string, or repeat until the result is short enough.
# method 1
answer = input("What's up, doc? ")[:10] # no more than 10 characters
# method 2
while True:
answer = input("What's up, doc? ")
if len(answer) <= 10:
break
else:
print("Too much info - keep it shorter!")
If that's not what you're asking, you need to make your question more specific.
You can get only the first n characters of the input text like so:
data = raw_input()[:10]
Hey guys so I’m slowly learning and I figured out you use the “\”. The thing is it adds a space to my string. So instead of: “The cat is black.” I get: “The cat is black.”
This is roughly what my code looks like. Sorry I’m on my phone. print(‘The cat is
black’)