# String manipulation in scripts

**URL:** <https://forum.inductiveautomation.com/t/string-manipulation-in-scripts/10218>\
**Category:** General Discussion\
**Created:** [July 14, 2015, 2:44pm UTC](https://forum.inductiveautomation.com/t/string-manipulation-in-scripts/10218 "2015-07-14T14:44:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcs226](https://avatars.discourse-cdn.com/v4/letter/l/848f3c/32.png) [@lcs226](https://forum.inductiveautomation.com/u/lcs226)\
**Post date:** [July 14, 2015, 2:44pm UTC](https://forum.inductiveautomation.com/t/string-manipulation-in-scripts/10218/1 "2015-07-14T14:44:06Z")

</div>

I am wondering if I am loosing my mind? Cannot get this to work

[color=#0080FF]import app, system

keyCode = ‘020-250’ #system.tag.getTagValue(’[Client]Tracking/AccessCode’)

length = len(keyCode)  
print “”  
print “Keycode Length -”,length  
if length \< 9:  
loop = 9 - length  
print “Missing -”,loop  
code = right(keycode, 3)

if length \>= 9:  
system.tag.writeToTag("[Client]Tracking/Timer\_Enable", 1)  
print “Key code -”,keyCode  
print “Jump to app.KeyCode.code”  
app.KeyCode.code(keyCode)[/color]

I am getting an error “NameError: name ‘right’ is not defined”. I thought you could user string manipulation in scripts not just expressions?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 14, 2015, 2:46pm UTC](https://forum.inductiveautomation.com/t/string-manipulation-in-scripts/10218/2 "2015-07-14T14:46:51Z")

</div>

No, you can’t use any expression functions in the scripting language, they’re totally separate.

Try some of the python string manipulations available instead: [docs.python.org/release/2.5.2/l … thods.html](https://docs.python.org/release/2.5.2/lib/string-methods.html)

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [July 14, 2015, 3:56pm UTC](https://forum.inductiveautomation.com/t/string-manipulation-in-scripts/10218/3 "2015-07-14T15:56:40Z")

</div>

Use Python’s [slicing notation](http://www.pythoncentral.io/cutting-and-slicing-strings-in-python/) to cut up strings any way you want.

Here’s your code with proper string slice and logic:

```auto
import app, system
keyCode = '020-250' #system.tag.getTagValue('[Client]Tracking/AccessCode')
length = len(keyCode)
print ""
print "Keycode Length -",length
if length < 9:
	loop = 9 - length
	print "Missing -",loop
	#code = right(keycode, 3)
	code = keyCode[-3:]
else:
	system.tag.writeToTag("[Client]Tracking/Timer_Enable", 1)
	print "Key code -",keyCode
	print "Jump to app.KeyCode.code"
	app.KeyCode.code(keyCode)
```
