Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Convert Integer into String in Python - DesignLinux

designlinux 0 Comments

Python has several built-in data types. Sometimes, when writing Python code, you might need to convert one data type to another. For example, concatenate a string and integer, first, you’ll need to convert the integer into a string.

This article explains how to convert a Python integer to a string.

Python str() Function #

In Python, we can convert integers and other data types to strings using the built-in str() function.

The str() function returns a string version of a given object. It takes the following forms:

class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')
  • object – Object to be converted to a string.

The function accepts three arguments, but usually, when converting an integer to a string, you’ll pass only one argument (object) to the function.

Converting a Python Integer into String #

To convert the integer 23 to a string version, simply pass the number into the str() function:

str(23)
type(days)
'23'
<class 'str'>

The quotes around 23 indicate that the number is not an integer but is an object of string type. Also, the type() function shows that the object is a string.

In Python, strings are declared using single ('), double ("), or triple quotes (""").

Concatenating Strings and Integers #

Let’s try to concatenate strings and integers using the + operator and print the result:

number = 6
lang = "Python"
quote = "There are " + number + " relational operators in " + lang + "."
print(quote)

Python will throw a TypeError exception error because it cannot concatenate strings and integers:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

To convert the integer to a string, pass the integer to the str() function:

number = 6
lang = "Python"
quote = "There are " + str(number) + " relational operators in " + lang + "."
print(quote)

Now when you run the code, it will be executed successfully:

There are 6 relational operators in Python.

There are also other ways to concatenate strings and numbers.

The built-in string class provides a format() method that formats a given string using an arbitrary set of positional and keyword arguments:

number = 6
lang = "Python"
quote = "There are {} relational operators in {}.".format(number, lang)
print(quote)
There are 6 relational operators in Python.

On Python 3.6 and later, you can use f-strings, which are literal strings prefixed with ‘f’ containing expressions inside braces:

number = 6
lang = "Python"
quote = f"There are {number} relational operators in {lang}."
print(quote)
There are 6 relational operators in Python.

Lastly, you can use the old %-formatting:

number = 6
lang = "Python"
quote = "There are %s relational operators in %s." % (number, lang)
print(quote)
There are 6 relational operators in Python.

Conclusion #

In Python, you can convert an integer to a string using the str() function.

If you have any questions or feedback, feel free to leave a comment.

python

Related

Tags: python

Sysmon – A Graphical System Activity Monitor for Linux

Prev Post

How to Install and Use Thonny Python IDE on Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 259
  • 614,631

DesignLinux.com © All rights reserved

Go to mobile version