Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python left() equivalent?

Tags:

python

django

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?

like image 921
GrantU Avatar asked Dec 03 '25 16:12

GrantU


2 Answers

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
like image 194
TerryA Avatar answered Dec 06 '25 06:12

TerryA


If you know it will always be col followed by some numbers:

>>> int('col1234'[3:])
1234
like image 25
Burhan Khalid Avatar answered Dec 06 '25 07:12

Burhan Khalid