ros python subscriber

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    
def listener():

    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("chatter", String, callback)

    rospy.spin()

if __name__ == '__main__':
    listener()

Here is what the above code is Doing:
1. The first line of the code imports the ROS Python client library.
2. The second line imports the message type that we will be using, std_msgs.msg.String.
3. The callback function is defined. It takes in a data of type std_msgs.msg.String and prints it to the console.
4. The listener function is defined. It initializes the node, creates a subscriber, and then spins to keep the node from exiting.
5. The if statement checks if the node has received a signal to shut down.
6. The listener function is called.