3

私はPythonとDjangoを学んでいます。

次の文字列 'col' の終了値のみを取得したい 終了値は常に数値、つまり 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する必要がありますか?

4

2 に答える 2

8

あなたはスライスを探しています:

>>> 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
于 2013-04-23T09:56:22.790 に答える