• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert String into Tuple in Python

Convert String into Tuple in Python

July 13, 2022 Leave a Comment

To convert a string into a tuple using Python, use the tuple() function.

string = "hello"

t = tuple(string)

print(t)

#Output:
('h', 'e', 'l', 'l', 'o')

When working with different objects in Python, the ability to be able to convert objects into other objects can be valuable.

One such situation is if you want to convert a string to a tuple.

To convert a string into a tuple using Python, use the tuple() function.

Below is a simple example of how you can convert a string into a tuple using tuple() in Python.

string = "hello"

t = tuple(string)

print(t)

#Output:
('h', 'e', 'l', 'l', 'o')

Convert Tuple to String in Python

To go the other way and convert a tuple into a string, it’s not quite as easy as just converting the tuple into a string with the Python str() function.

To convert a tuple into a string, you can instead use the string join() function.

Below is a simple example of how you can convert a tuple into a string in Python.

t = ('h', 'e', 'l', 'l', 'o')

s = "".join(t)

print(s)

#Output:
hello

Using tuple() to Convert Other Objects to Tuples in Python

You can also use the Python tuple() function to convert other objects into tuples.

One such example is if you want to convert a list into a tuple in your Python code.

To do so, you can slightly modify the example from above.

Below is a simple example showing you how to convert a list into a tuple using Python.

lst = [0, 1, 2, 3]

t = tuple(lst)

print(t)

#Output:
(0, 1, 2, 3)

Hopefully this article has been useful for you to learn how to convert a string into a tuple in Python.

Other Articles You'll Also Like:

  • 1.  How to Slice a Dictionary in Python
  • 2.  Using Python to Print Plus or Minus Sign Symbol
  • 3.  Get Name of Function in Python
  • 4.  Using Python to Calculate Average of List of Numbers
  • 5.  pandas product – Get Product of Series or DataFrame Columns
  • 6.  Check if Variable is Integer in Python
  • 7.  Get pandas Series Last Element in Python
  • 8.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 9.  How to Group By Columns and Find Minimum in pandas DataFrame
  • 10.  Remove Specific Word from String in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy