How to convert a list to a dictionary in Python?

Problem To convert a simple or nested list into a dictionary data object using Python. Solution In this tutorial we’ll focus on usage of dictionary comprehensions in order to add the content of a Python list iterator into a dictionary. 1. Dict from Python list Let’s assume that we have the following list: We can … Read more

How to remove newlines from a string in Python 3?

Problem You have a very long string or one that include newline escape characters (\n). You would like to use Python 3 in order to automatically delete those appended newlines from your string, list or file. Strip newlines / line breaks from a Python string In this post, we will outline three methods that you … Read more

How to print a string in reverse in Python 3?

Problem We would like to turn upside down a string and print it in reverse mode in Python. Solution We have many different methods to approach to reverse a string in Python. Let’s look at each technique one by one in this post. Using Extended Slicing technique: We can obtain with the reverse of a … Read more

How to open and read an excel file with Python?

Problem Working with spreadsheets is essential when dealing with data. In this tutorial we would like to show you how to open and read excel worksheets (xls / xlsx file formats) using Python 3. Solution We have two libraries in Python that can help us to work with the excel sheet. We will discuss more … Read more

How to write text files in Python 3?

Task: A very common automation task that we tackle with Python is to create text files as well as read, write and save data into those files. Solution: Python delivers very powerful built-in functions to create and manipulate text files. In this short tutorial we’ll go through several key capabilities: Create a new text file … Read more

How to convert a Python string to Hex format?

Problem To convert a string data type to an Hexadecimal number format in Python 3.X. Solution We have two methods to convert a python string to a Hex value. Using int() function and by importing literal_eval from the ast Python module. In this post, we will learn how we can convert a string to a Hexadecimal number.  … Read more

How to round up a float number in Python?

Problem: Today, we’ll learn how to round up float numbers using Python. Solution: We have two methods to handle the rounding float numbers in Python. Using the round() function and Decimal module. We will look at the solutions with examples for each method in this post. Option 1: Python Round Function Python has an inbuilt function round(), which … Read more