Yaswanth Varma

Yaswanth Varma

307 Articles Published

Articles by Yaswanth Varma

Page 5 of 31

Why is indentation important in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 1K+ Views

Indentation indicates the spaces or tabs placed at the beginning of a line of code to indicate the block structure. In many programming languages, indentation is used to improve code readability. In Python, indentation is the key part of the syntax. It is used to define the blocks of code, such as loops, conditionals, and functions. If indentation is not used properly in Python, it results in the IndentationError, causing the program to fail. Code Without Proper Indentation Let's see what happens when we use an if-else statement without proper indentation ? a = 5 ...

Read More

How to retrieve Python module path?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 2K+ Views

In Python, every module exists in a specific file path within the file system. Sometimes, we need to find out where the module is located to perform different operations, like debugging or verifying the version of the module. In this article, we will explore how to retrieve the file path of a module. Using __file__ Attribute The most common way to retrieve a module's path is using the __file__ attribute. This attribute contains the pathname of the file from which the module was loaded. Standard Library Module Let's retrieve the path of the built-in os module, ...

Read More

How can I get a list of locally installed Python modules?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 4K+ Views

Python is a flexible programming language with thousands of libraries and modules. As your Python environment grows with new installations, it becomes important to check which packages are currently available. Whether you're debugging, documenting your environment, or managing dependencies, listing locally installed Python modules is a valuable skill. In this article, we will explore different ways to get a list of locally installed Python modules using built-in tools and command-line utilities. Using pip list Command The pip package installer provides a list subcommand that displays all installed packages in your current Python environment. This is the most ...

Read More

What is the difference between dir(), globals() and locals() functions in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 951 Views

The Python built-in functions, dir(), globals(), and locals() are used to provide insights into the objects, variables, and identifiers present in various scopes. They might look similar, but each function serves a different purpose and behaves differently depending on where and how it is used. Python dir() Function The Python dir() function is used to list the names in the current local scope or the attributes of an object. If no argument is passed, it returns the list of names in the current local scope. dir(object) Example Let's look at the following ...

Read More

How to import a single function from a Python module?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 10K+ Views

In Python, a module is a file containing Python definitions and statements. For keeping code organized, we often write functions in separate modules and import them into the main program. While importing the entire module, sometimes we only need a single function from it. Python provides a direct way to do this using the from module_name import function_name syntax, which is more memory-efficient and cleaner than importing the whole module. Syntax from module_name import function_name This imports only the specified function, making it available directly without the module prefix. Importing sqrt() from math ...

Read More

How to detect vowels vs consonants in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 7K+ Views

In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants. In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions. Using Python in Operator The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false. To detect whether a letter ...

Read More

How to delete consonants from a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 1K+ Views

The consonants are the alphabetic characters that are not vowels (a, e, i, o, u). In Python, string manipulation is a common task. In this article, we will explore how to delete consonants from a string using different approaches, including loops, list comprehensions and regular expressions. Using Loop In this approach, we declare a variable containing vowels in both upper and lower case, then loop through each character in the string. If the character is a vowel or a non-alphabet character, we add it to the result ? Example Let's look at the following example, where ...

Read More

How do I wrap a string in a file in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 3K+ Views

String wrapping means formatting or breaking text so that it fits within a specified width. This is useful when writing content to files, such as logs, reports, or console-like outputs that need consistent line lengths. Python provides built-in methods through the textwrap module to wrap strings before writing them to files. Let's explore different approaches to wrap strings in files. Using textwrap.wrap() Method The textwrap.wrap() method splits a single paragraph into a list of lines, each at most a specified width. Syntax textwrap.wrap(text, width) Example Here's how to wrap a string ...

Read More

How to correctly sort a string with a number inside in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 3K+ Views

Sorting strings that contain numbers, such as ("xy1", "xy2", "xy10"), can be complex in Python. For example, if we sort the list ["xy1", "xy2", "xy10"] using the built-in sort() method, it results in ["xy1", "xy10", "xy2"]. But we expect "xy2" to come before "xy10". This happens because Python's default sorting uses lexicographical order and compares characters from left to right based on their Unicode values. Since the character '1' in "xy10" comes before "2" in "xy2", the "xy10" is treated as smaller, even though the number 10 is greater than 2. This is where we need natural sorting, ...

Read More

How to replace the last occurrence of an expression in a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 8K+ Views

In this article, we are going to learn how to replace the last occurrence of an expression in a string. In Python, String manipulation is a common task, and Python provides the built-in method named replace(). Though we can replace the specified character or substring in a string using this method, it does not directly support replacing the last occurrence. To achieve this we need to use slicing, string splitting, or a regular expression. Using rfind() Method The rfind() method searches for the starting index of the last occurrence of the specified substring. We can use this ...

Read More
Showing 41–50 of 307 articles
« Prev 1 3 4 5 6 7 31 Next »
Advertisements