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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Using Python to Split String into Dictionary

Using Python to Split String into Dictionary

June 2, 2022 Leave a Comment

To split a string into a dictionary using Python, the easiest way is to use the Python split() function and dictionary comprehension. With this method, you will get a dictionary where the keys are numbers and the values are the words of the string.

string = "this is a string with some words"

d = { idx: ele for idx, ele in enumerate(string.split())}

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

You can also use a loop explicitly to create a dictionary from a string.

string = "this is a string with some words"

d = dict()

for idx, ele in enumerate(string.split()):
    d[idx] = ele

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

When working with strings and text in Python, the ability to manipulate and create new objects from strings can be useful.

One such situation is if you want to take a string and create a dictionary with the words from the string. You can split a string into a dictionary easily with the Python split() function and dictionary comprehension.

Given a string, you can use split() to split a string by a delimiter into a list.

Then, you can use enumerate() to get the index and values of the list of words and use dictionary comprehension to create a new dictionary.

Below is an example of how you can split a string into a dictionary using Python.

string = "this is a string with some words"

d = { idx: ele for idx, ele in enumerate(string.split())}

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

How to Split String into Dictionary of Words Using Python

Above is a one line solution to the question of how to split a string into a dictionary of words in your Python code.

You can be more explicit and use a for loop if there are any other manipulations or additive things you need to add to your dictionary.

Below is another example of how to split a string into a dictionary.

Notice we are still using split() and enumerate(). The difference now is that we are building the dictionary from scratch.

string = "this is a string with some words"

d = dict()

for idx, ele in enumerate(string.split()):
    d[idx] = ele

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

Hopefully this article has been useful for you to learn how to split a string into a dictionary using Python.

Other Articles You'll Also Like:

  • 1.  Python Factorial Recursion – Using Recursive Function to Find Factorials
  • 2.  pandas mean – Get Average of Series or DataFrame Columns
  • 3.  pandas product – Get Product of Series or DataFrame Columns
  • 4.  Create Empty File with Python
  • 5.  How to Rotate a List in Python
  • 6.  Return Multiple Values from Function in Python
  • 7.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 8.  Create Symbolic Link with Python os.symlink() Function
  • 9.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 10.  Log Base 10 Python – Find Logarithm of Number with Base 10 with log10()

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