I'm just learning Python and Django.
I want to get only the end value of the following string 'col' the end value is always a number i.e. col1, col2 etc
In other languages I could do this many ways...
left(value,3) - only leave the value after 3.
findreplace(value, 'col', '') - fine the string col replace with blank leaving nothing but the number I need.
So my question is, within Django (Python) how can I do these things? (in a view not a template)
Also is Django strict? will I need to int the value to make it a number after?
You're looking for slicing:
>>> s = "Hello World!"
>>> print s[2:] # From the second (third) letter, print the whole string
llo World!
>>> print s[2:5] # Print from the second (third) letter to the fifth string
llo
>>> print s[-2:] # Print from right to left
d!
>>> print s[::2] # Print every second letter
HloWrd
So for your example:
>>> s = 'col555'
>>> print s[3:]
555
If you know it will always be col followed by some numbers:
>>> int('col1234'[3:])
1234
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