Python: If Then with Left Equivalent

555
6
Jump to solution
10-24-2017 07:35 AM
MichaelWalden
New Contributor

Codeblock

Trying to calculate using Python in 10.3 the "Region" field with text based on the first two characters in a string field called "API_10".  Basically, if the left two characters in "API_10" equal "33", then populate "Region" with "North".

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

API_10[0:2] or API_10[:2]

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

API_10[0:2] or API_10[:2]

JoshuaBixby
MVP Esteemed Contributor

Also, just pass the whole column to your function since the function does slicing and checking:  Region = TextValue(!API_10!)

0 Kudos
MichaelWalden
New Contributor

Thanks Joshua!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Joshua is correct... python slicing is 0 based and exclusive of the slice number, hence

whatever[:2] or whatever[0:2] will return 0 and 1.  The slice is [start:stop:step]

whatever = 'abcdefg'

whatever[0:6:2]
'ace'

whatever[:6:2]
'ace'

or skipping the start and stop

whatever[:6]
'abcdef'
TedKowal
Occasional Contributor III

When you do a slice or substring in Python and  it starts with "0" then the second number is always the length.  This confused me for a while ..... https://www.dotnetperls.com/substring-python 

0 Kudos
DanPatterson_Retired
MVP Emeritus

Or the most succinct descriptor... the python docs 

0 Kudos