Python String format_map()

Formats the string according to the Format Description Language, passing a mapping object. Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.format_map(mapping) Formats the string according to the Format Description Language, passing a mapping object. In that way, it … Read more

Python String index()

Returns the index of the first occurrence of the specified substring, like find() but it raises a ValueError if the substring is not found. Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.index(sub[, start[, end]]) Returns the index of … Read more

Python String capitalize()

Return a copy of the string with capitalized first character and lowercased remaining characters. Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.capitalize() Creates a new string with capitalized first character, and lowercased remaining characters. Note that this function … Read more

Python String Methods

Method Description capitalize() Return a copy of the string with capitalized first character and lowercased remaining characters. casefold() Return a lowercased, casefolded string similar to lowercase() but more aggressive. center() Return a centered string of a certain length, padded with whitespace or custom characters. count() Return the number of non-overlapping occurrences of a substring. encode() … Read more

Python Celery – How to Get a Task Result Object by ID?

What is Celery? Celery allows you to scale your application by distributing processing workload among multiple worker machines or processes. Celery uses task queues as units of work. A dedicated worker process monitors those task queues and pulls new work from those if it becomes available. Clients add messages to the task queue and brokers … Read more

How to Check If a Python List is Empty?

Believe it or not—how you answer this question in your day-to-day code reveals your true Python skill level to every master coder who reads your code. Beginner coders check if a list a is empty using crude statements like len(a)==0 or a==[]. While those solve the problem—they check if a list is empty—they are not … Read more

How to Loop Through a Python List in Pairs, Sliding Windows, and Batches?

Method 1: Iterating over Consecutive (Sliding) Windows Given are: Python list lst Window size n Problem Formulation: How to loop through the list in consecutive element-windows of size n, so that in each iteration, you can access the n next elements in the list? # INPUT: lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’] … Read more

How to Call a Parent’s Class Method in Python?

Problem Formulation: Calling Parent Methods Given Parent class P Child class C that inherits from P Class method P.my_method() defined on the parent P Problem: Within a method of class C, how to call the parents method P.my_method()? Example: Consider the following example where you create a parent and a child class. You want to … Read more

How to Open Outlook in Python?

Problem Formulation Given a Windows operating system on which you have the Microsoft Outlook email program installed. How to open Microsoft Outlook using only a function call in your Python script? Solution: os.startfile(‘outlook’) The easiest way to open Outlook on Windows using only a short and concise Python One-Liner is to use the os.startfile(‘outlook’) function … Read more

How to rm -rf in Python to Delete a Directory?

Problem Formulation: How to Remove a Directory in Python? The rm command in Linux removes a specific directory. You can also add the options -r remove the directory recursively -f ignore nonexistent files and arguments and don’t prompt the user to ask for confirmation So, if you run rm -rf my_directory, it’ll forcefully remove my_directory … Read more

What are the Applications of Graphs in Computer Science?

[Reading time: 9 minutes] Graphs are everywhere. They are used in social networks, the world wide web, biological networks, semantic web, product recommendation engines, mapping services, blockchains, and Bitcoin flow analyses. Furthermore, they’re used to define the flow of computation of software programs, to represent communication networks in distributed systems, and to represent data relationships … Read more