

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change any data type into a string in Python?
<p>Any built-in data type converted into its string representation by str() function</p> <pre class="prettyprint notranslate"> >>> str(10) '10' >>> str(11.11) '11.11' >>> str(3+4j) '(3+4j)' >>> str([1,2,3]) '[1, 2, 3]' >>> str((1,2,3)) '(1, 2, 3)' >>> str({1:11, 2:22, 3:33}) '{1: 11, 2: 22, 3: 33}' </pre> <p>For a user defined class to be converted to string representation, __str__() function needs to be defined in it.</p> <pre class="prettyprint notranslate"> >>> class rectangle: def __init__(self): self.l=10 self.b=10 def __str__(self): return 'length={} breadth={}'.format(self.l, self.b) >>> r1=rect() >>> str(r1) 'length = 10 breadth = 10' </pre>
- Related Questions & Answers
- Python Pandas - Convert string data into datetime type
- String Data Type in Python
- Change data type of given numpy array in Python
- PHP String Data Type
- Change data type of given numpy array
- How to convert JSON data into a Python tuple?
- How to convert JSON data into a Python object?
- C++ Program for sorting variables of any data type
- How to convert from string to date data type in MongoDB?
- How to change array into a list in Java?
- How to write data into BLOB and CLOB type columns in a table using JDBC?
- Number Data Type in Python
- List Data Type in Python
- Tuple Data Type in Python
- Dictionary Data Type in Python
Advertisements