2013-04-23 110 views
1

我只是在学习Python和Django。Python左()相当于?

我想只得到以下字符串“关口”的终值终值始终是一个数,即COL1,COL2等

在其他语言中我能做到这一点很多方面......

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. 

所以我的问题是,Django(Python)中的我该怎么做?(在视图中不是模板)

又是Django严格吗?我是否需要int值来使它成为一个数字?

+3

这是称为“切片”,并且在Python教程中:例如:value [3:] - 将取3个字符向前...(基于0索引) – 2013-04-23 09:47:47

+0

@Jon Clements'切片'很容易,当你知道它叫什么!那么你能从左到右吗? – GrantU 2013-04-23 09:50:20

+0

@ User7是的!并从右到左使用负整数:)。 – TerryA 2013-04-23 09:50:57

回答

3

您正在寻找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 

因此,对于你的例子:

>>> s = 'col555' 
>>> print s[3:] 
555 
2

如果你知道它永远是col其次是一些数字:

>>> int('col1234'[3:]) 
1234