Open In App

Python Tweepy – Getting the screen name of a user

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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


Last Updated : 03 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads