Someone gave me a syntax to truncate a string as follows:
string = "My Text String" print string [0:3] # This is just an example
I'm not sure what this is called (the string[0:3] syntax), so I've had a hard time trying to look it up on the internet and understand how it works. So far I think it works like this:
Anyways, there's probably a few other examples that I can add, but my point is that I'm new to this functionality and I'm wondering what it's called and where I can find more information on this. I'm sure I'm just missing a good reference somewhere.
Thanks for any suggestions, Mike
Truncate the string (first argument) if it is longer than the given maximum string length (second argument) and return the truncated string with a ... ending. The inserted three dots at the end should also add to the string length.
Python File truncate() Method The truncate() method resizes the file to the given number of bytes. If the size is not specified, the current position will be used.
Truncation in IT refers to “cutting” something, or removing parts of it to make it shorter. In general, truncation takes a certain object such as a number or text string and reduces it in some way, which makes it less resources to store.
strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
It's called a slice. From the python documentation under Common Sequence Operations:
s[i:j]
The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With