ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Question about one sentence

asked 2016-03-29 20:49:36 -0500

junjun gravatar image

updated 2016-03-30 01:00:38 -0500

mgruhler gravatar image

I got this sentence from the ros tutorial. It's located at helloworld\follow_the_route.py line 43:

    # Navigation
    rospy.loginfo("Go to %s pose", name[:-4])
    success = navigator.goto(obj['position'], obj['quaternion'])
    if not success:
        rospy.loginfo("Failed to reach %s pose", name[:-4])
        continue
    rospy.loginfo("Reached %s pose", name[:-4])

I can't understand the sentence,

 rospy.loginfo("Go to %s pose", name[:-4])

What is name[:-4]?

I think name is enough to represent a string. what is [:-4]? What is the syntax here?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2016-03-30 00:59:50 -0500

mgruhler gravatar image

updated 2016-03-30 01:07:07 -0500

This is rather a python than a ROS question.

With the syntax name[:-4] you get all but the last four elements of the sequence name.

You can easily test in a python console. E.g.

>>> name = 'ThisIsMyName'
>>> print name[:-4]
ThisIsMy

(Next time, please use the preformatted text button, the one with 1s and 0s, to format your question in a more readable format).


EDIT

A bit more background: This is also called slicing.

Whereas with most mutable sequences (like e.g. lists) you can change the sequence this way as well, strings are immutable and thus you cannot change them. For more info, check out this article about slicing strings, as well as the python doc about mutable sequence types.

edit flag offensive delete link more

Comments

And for additional information, this is called as "slicing" in python. https://docs.python.org/2/tutorial/in...

Akif gravatar image Akif  ( 2016-03-30 01:03:38 -0500 )edit

whoops, beat me by one minute :-D added some links as well...

mgruhler gravatar image mgruhler  ( 2016-03-30 01:05:55 -0500 )edit

You beat me on answer, so I added it as comment :) Though, it is a comprehensive answer now without my comment.

Akif gravatar image Akif  ( 2016-03-30 01:15:09 -0500 )edit

Thank you for your answer. Comprehensive.

junjun gravatar image junjun  ( 2016-03-30 08:47:36 -0500 )edit

Question Tools

Stats

Asked: 2016-03-29 20:49:36 -0500

Seen: 224 times

Last updated: Mar 30 '16