0

I've got a string test_string like 'Hello, two is a number'" and I want to copy the characters between position 7 and 12. In the programming language I use:

test_string = 'Hello, two is a number'
new_string = string_copy(test_string, 7, 12)

the value of new_string would be ' two i'. My example might be a bit stupid, but does a function exist in Python equal to string_copy?

Joost Verbraeken
  • 883
  • 2
  • 15
  • 30

1 Answers1

0

This is a built-in feature, that’s called slicing:

'Hello, two is a number'[7:12]

or

test_string = 'Hello, two is a number'
new_string = test_string[7:12]

You seem to have miscounted though, as the result will be 'two i' (without leading space).

edit: Or, as Zero Piraeus points out, your language uses 1-based indexing in contrast to python

See also https://docs.python.org/3/tutorial/introduction.html#lists

wonce
  • 1,893
  • 12
  • 18
  • That should be `'Hello, two is a number'[6:12]` ... question assumes 1-based indexing and closed [intervals](http://en.wikipedia.org/wiki/Interval_%28mathematics%29#Terminology) (which is reasonable, just not how Python does it), whereas Python's slicing is [zero-based](http://en.wikipedia.org/wiki/Zero-based_numbering) and half-open. – Zero Piraeus Jul 05 '14 at 21:23
  • 1
    Thanks for the answer and your method works like a charm. I checked the GML (Game Maker Language) documentation which uses 1-based indexing indeed. – Joost Verbraeken Jul 05 '14 at 22:52