I can't easily find out the exact size of the string I will produce. I only know the upper bound which should be within 1-2 characters of the final size. How do I shrink the string after filling it?
Asked
Active
Viewed 94 times
0
-
1You don't. In C you could use realloc, but python does not expose memory micro management routines. The real question is, why would you care about a few bytes anyway? – Clarus Jul 02 '14 at 16:57
-
Also python does some interning for small strings, so what you want to do might actually use *more* memory. – Bakuriu Jul 02 '14 at 18:15
-
1@Claris 'asdf' != 'asdf\0\0' – Hristo Venev Jul 03 '14 at 06:30
1 Answers
0
Based on your comment:
- If you are trying to remove characters from your string use the .strip() method.
- If you want the byte count of the string compared to the character count you need to change the encoding.
- If you are just trying to remove the \0 character use the .replace() method.

Clarus
- 2,259
- 16
- 27
-
I know that I will make at most 1005 ASCII characters. I allocate a 1005-character ASCII string. I fill 1003 characters. How do I make the string 1003 characters long without reallocating? – Hristo Venev Jul 04 '14 at 15:01