Skip to content

Programming Articles

  • HOME
  • Python
  • JavaScript
  • C/C++
  • Java
  • Terms & Conditions and Privacy Policy
  • About US
Main Menu

Python HowTos

Python / Python HowTos

How to pad a string with zeroes to the left?

This is a very frequently asked question, how do I pad a numeric string with zeroes to the left, so that the string has a specific length? In this article, …

How to pad a string with zeroes to the left? Learn More
Python / Python HowTos

How to delete an element from a dictionary?

The del statement removes an element: Note that this mutates the existing dictionary, so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return …

How to delete an element from a dictionary? Learn More
Python HowTos

How to count the occurrences of a list item in Python?

If you only want a single item’s count, use the count method: Important: this is very slow if you are counting multiple different items Each count call goes over the entire list of n elements. Calling count in a loop n times …

How to count the occurrences of a list item in Python? Learn More
Python HowTos

How to remove an element from a list by index in Python?

Use del and specify the index of the element you want to delete: Also supports slices: How to remove an element from a list by index in Python? You probably want pop: By …

How to remove an element from a list by index in Python? Learn More
Python HowTos

How to determine the type of an object in Python?

There are two built-in functions that help you identify the type of an object. You can use type() if you need the exact type of an object, and isinstance() to check an object’s type against something. …

How to determine the type of an object in Python? Learn More
Python HowTos

How can I print literal curly-brace characters in a string and also use .format on it in Python?

You need to double the {{ and }}: Here’s the relevant part of the Python documentation for format string syntax: Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained …

How can I print literal curly-brace characters in a string and also use .format on it in Python? Learn More
Python HowTos

How to import files from different folders in Python?

Sample query: I have the following folder structure. I want to import some functions from file.py in some_file.py. I’ve tried and some other various attempts but so far I couldn’t manage to import …

How to import files from different folders in Python? Learn More
Python / Python HowTos

How to move a file in Python?

Although os.rename() and shutil.move() will both rename files, the command that is closest to the Unix mv command is shutil.move(). The difference is that os.rename() doesn’t work if the source and destination are on different disks, while shutil.move() is …

How to move a file in Python? Learn More
Python HowTos

How to copy a file in Python?

In this post, we’ll discuss how to copy a file in Python with code examples. How to copy a file in Python? shutil has many methods you can use. One of …

How to copy a file in Python? Learn More
Python HowTos

How to check if a list is empty in python?

Query: For example, if passed the following: How do I check to see if a is empty? How to check if a list is empty in Python? This can be done using …

How to check if a list is empty in python? Learn More
Formatted Codes / Python HowTos

How to catch multiple exceptions in one line in Python?

Query explained: I know that I can do: I can also do this: But if I want to do the same thing inside two different exceptions, the best I can …

How to catch multiple exceptions in one line in Python? Learn More
Formatted Codes / Python HowTos

How to list all files of a directory in Python?

Query: How can I list all files of a directory in Python and add them to a list? How to list all files of a directory in Python? Method #1: os.listdir() will …

How to list all files of a directory in Python? Learn More
Python HowTos

How to use global variables in a function in Python?

Query: How can I create or use a global variable in a function? If I create a global variable in one function, how can I use that global variable in …

How to use global variables in a function in Python? Learn More
Formatted Codes / Python HowTos

How to iterate over dictionaries using ‘for’ loops in Python?

Query explained: I am a bit puzzled by the following code: What I don’t understand is the key portion. How does Python recognize that it needs only to read the key from …

How to iterate over dictionaries using ‘for’ loops in Python? Learn More
Formatted Codes / Python HowTos

How to find the index of an item in a list in Python?

The following code is the solution: Caveats follow Note that while this is perhaps the cleanest way to answer the question as asked, index is a rather weak component of the list API, and I …

How to find the index of an item in a list in Python? Learn More
Python HowTos

How to access the index in ‘for’ loop? [Answered]

Sample query: How do I access the index in a for loop like the following? I want to get this output: When I loop through it using a for loop, how do I access …

How to access the index in ‘for’ loop? [Answered] Learn More
Formatted Codes / Python HowTos

How to safely create a nested directory in Python? [Answered]

Query explained: What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? …

How to safely create a nested directory in Python? [Answered] Learn More

Posts navigation

1 2 Next

Featured Posts

From UNIX to Linux – History of Linux

21st November 202010th September 2022

Best themes for Ubuntu: Make Ubuntu Beautiful

20th October 202010th September 2022

Strings in Python

12th September 202010th September 2022

Is Python hard to learn? Top best learning resources

18th April 202010th September 2022

Recent posts

  • How to pad a string with zeroes to the left?
  • What is the difference between process and thread?
  • How to generate a random alpha-numeric string in Java?
  • How to auto-resize an image to fit a ‘div’ container?
  • How to get a Docker container’s IP address from the host?
  • How to display a JavaScript object?
Copyright © 2023 Programming Articles.
Powered by WordPress and HitMag.