Skip to content
Related Articles

Related Articles

Improve Article
Python Tweepy – Getting the screen name of a user
  • Last Updated : 03 Jul, 2020

In this article we will see how we can get the screen name of a user. Screen name is the username of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their screen name, often in a shortened version. All the screen names must be unique.

Identifying the screen name in the GUI :

In the above mentioned profile, geeksforgeeks is the screen name of the profile.

In order to get the screen name we have to do the following :

  1. Identify the user ID of the profile.
  2. Get the User object of the profile using the get_user() method and the user ID.
  3. From this object, fetch the screen_name attribute present in it.

Example 1: Consider the following profile :

The user ID of the above mentioned profile is 57741058.






# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# the ID of the user
id = 57741058
  
# fetching the user
user = api.get_user(id)
  
# fetching the screen name
screen_name = user.screen_name
  
print("The screen name of the user is : " + screen_name)

Output :

The screen name of the user is : geeksforgeeks

Example 2: Consider the following profile :

The user ID of the above mentioned profile is 4802800777.




# the ID of the user
id = 4802800777
  
# fetching the user
user = api.get_user(id)
  
# fetching the screen name
screen_name = user.screen_name
  
print("The screen name of the user is : " + screen_name)

Output :

The screen name of the user is : PracticeGfG

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :