Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming. A string is a sequence of characters. Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes.
Example : Creating strings in different ways :
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again
Changing any data type into a String
There are two ways for changing any data type into a String in Python :
- Using the
- Using the
Method 1 : Using the
Any built-in data type can be converted into its string representation by the
str() function. Built-in data type in python include:-
str(built-in data type)
Type before : <class 'int'> Type after : <class 'str'> Type before : <class 'float'> Type after : <class 'str'> Type before : <class 'list'> Type after : <class 'str'> Type before : <class 'tuple'> Type after : <class 'str'>
Method 2 : Defining
__str__() function for a user defined class to be converted to string representation. For a user defined class to be converted to string representation,
__str__() function needs to be defined in it.
value of a =10 value of b =10 <class 'str'>
- Change data type of given numpy array
- Change Data Type for one or more columns in Pandas Dataframe
- Change the data type of a column or a Pandas Series
- Python - Change type of key in Dictionary list
- Python - Convert Tick-by-Tick data into OHLC (Open-High-Low-Close) Data
- How to Change a Dictionary Into a Class?
- Python | Check if string ends with any string in given list
- Data type Object (dtype) in NumPy Python
- Python | Pandas Series.astype() to convert Data type of series
- Python | Working with the Image Data Type in pillow
- Python | Check order specific data type in tuple
- Python - Matrix Data Type Rectification
- Python - Test Similar Data Type in Tuple
- Python - Remove particular data type Elements from Tuple
- Get the data type of column in Pandas - Python
- Python - Specific case change in String List
- How to Check the Data Type in Pandas DataFrame?
- Converting WhatsApp chat data into a Word Cloud using Python
- Load CSV data into List and Dictionary using Python
- How to Parse Data From JSON into Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.