With a 64-bit Python installation, and (say) 64 GB of memory, a Python string of around 63 GB should be quite feasible, if not maximally fast. If you can upgrade your memory beyond 64 GB, your maximum feasible strings should get proportionally longer. (I don't recommend relying on virtual memory to extend that by much, or your runtimes will get simply ridiculous;-).
With a typical 32-bit Python installation, the total memory you can use in your application is limited to something like 2 or 3 GB (depending on OS and configuration), so the longest strings you can use will be much smaller than in 64-bit installations with high amounts of RAM.
Answer from Alex Martelli on Stack OverflowWith a 64-bit Python installation, and (say) 64 GB of memory, a Python string of around 63 GB should be quite feasible, if not maximally fast. If you can upgrade your memory beyond 64 GB, your maximum feasible strings should get proportionally longer. (I don't recommend relying on virtual memory to extend that by much, or your runtimes will get simply ridiculous;-).
With a typical 32-bit Python installation, the total memory you can use in your application is limited to something like 2 or 3 GB (depending on OS and configuration), so the longest strings you can use will be much smaller than in 64-bit installations with high amounts of RAM.
I ran this code on an x2iedn.16xlarge EC2 instance, which has 2048 GiB (2.2 TB) of RAM
>>> one_gigabyte = 1_000_000_000
>>> my_str = 'A' * (2000 * one_gigabyte)
It took a couple minutes but I was able to allocate a 2TB string on Python 3.10 running on Ubuntu 22.04.
>>> import sys
>>> sys.getsizeof(my_str)
2000000000049
>>> my_str
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
The last line actually hangs, but it would print 2 trillion As.
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've got to get an input function that I need to input a large string into. However, I realised that the max amount of characters I can input is 1023 and any more characters wold cause the input function to not accept it. Why is this so, and how can I fix it
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]
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'