Yaswanth Varma

Yaswanth Varma

307 Articles Published

Articles by Yaswanth Varma

Page 8 of 31

Why substring slicing index out of range works in Python?

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

In many programming languages, trying to access an element that is out of range of a string or list results in an IndexError. But Python behaves differently when it comes to substring slicing. Instead of generating an error, Python handles out-of-range indices in slicing operations by automatically adjusting them to fit within valid limits. This makes Python slicing safe and forgiving ? How Python Handles Out-of-Range Slicing When you use slicing with indices that exceed the string boundaries, Python applies these rules: If the start index is negative and beyond the string start, it's treated ...

Read More

How to process escape sequences in a string in Python?

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

Escape sequences are special characters that represent whitespace characters, quotes, backslashes, or non-printable characters within strings. These sequences start with a backslash followed by a character, for example represents a newline. Sometimes you need to process or interpret these escape sequences in your strings. This article demonstrates different approaches to handle escape sequences in Python. Basic Escape Sequences Let's first see how escape sequences work normally in Python strings − text = "Hello\tEveryoneWelcome to TutorialsPoint" print(text) The output of the above code is − Hello Everyone Welcome to TutorialsPoint ...

Read More

How to remove empty strings from a list of strings in Python?

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

The Lists in Python are used to store collections of items such as numbers, strings. While working with lists of strings, it is common to find empty strings in the list. An empty string is a string value with zero length (""). While they are present as elements in the list, they do not hold any meaningful data and can interfere with operations like sorting and filtering. Removing empty strings ensures the list contains only valid values. Python provides several methods to remove empty strings from a list. Let's explore the most effective approaches ? Using filter() ...

Read More

What is the most elegant way to check if the string is empty in Python?

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

The strings are fundamental data types used to store text. When working with strings, checking if a string is empty is a common task. An empty string is a string with zero characters. There are various ways to check if a string is empty, but choosing elegant approaches helps improve code readability. Using if not string (Most Pythonic) In Python, empty strings are considered falsy, which evaluates to False in a boolean context. Using not string returns True if the string is empty ? my_string = "" if not my_string: print("The ...

Read More

How to check if multiple strings exist in another string in Python?

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

While working with strings, checking if a string exists inside another string is a common task and can be achieved using the in keyword. In this article, we'll explore different methods to check if multiple strings exist in another string in Python. For example, checking if multiple strings like "TP" and "WELCOME" exist in the string "WELCOME to TP". Python provides several approaches including any(), all(), and regular expressions. Using any() Function The any() function returns True if at least one element in an iterable is truthy. We can use it with a generator expression to check ...

Read More

How to get a function name as a string in Python?

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

In Python, functions are first-class objects, which means they can be passed around and manipulated like other data types (integers, strings, etc.). This allows us to inspect and retrieve function attributes, including their names as strings. To get a function name as a string, we use the built-in __name__ attribute that every function object possesses. The __name__ Attribute The __name__ attribute is a special built-in variable that stores the name of a function, class, or module as a string. For functions, it contains the original name used when defining the function. Basic Usage Here's how ...

Read More

How can we change the id of an immutable string in Python?

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

In Python, strings are immutable, which means once the string is created, it cannot be changed. Because of this immutability, any operation that tries to modify the string results in creating a new string object with a different identity. Every object in Python has a unique identity, which can be accessed by using the built-in id() function. In this article, we are going to learn about changing the id of an immutable string in Python. We cannot directly change the ID of the immutable string, but by reassigning or modifying it, we can create a new string and ...

Read More

How to get a variable name as a string in Python?

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

In Python, variables are just labels pointing to values. They don't carry any built-in metadata about their names, so there's no direct method to get a variable name as a string. However, we can achieve this by inspecting the environment using globals(), locals(), or the inspect module. Let's explore these approaches ? Using globals() Function The globals() function returns a dictionary representing the current global symbol table. We can search through this dictionary to find variable names by their values. Syntax globals() Example Here's how to find a global variable name ...

Read More

How to convert hex string into int in Python?

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

A Hexadecimal is the base-16 number system that uses the digits from '0 to 9' and letters from 'A to F'. It is commonly used in computers to represent binary data in a readable format. In this article, we will learn about converting hex strings into integers. For example, if we receive input as a hex string ("fe00") and need to convert it into an integer, Python provides multiple methods. Let's explore them one by one. Using int() Function The int() function is the most common method to convert a hex string to an integer. It accepts ...

Read More

What does the \'b\' character do in front of a string literal in Python?

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

A string is a collection of characters that can represent a single word or a complete phrase. Since you can directly assign strings in Python to a literal (unlike other technologies) it is easy to use them. The string literals are typically enclosed in quotes (' ' or " ") and they represent sequences of characters. However, in some scenarios we will encounter a string that starts with a lowercase b for example, b'WELCOME', where the b prefix stands for bytes literal. Bytes literals are useful when dealing with binary data, where data is transmitted or received in ...

Read More
Showing 71–80 of 307 articles
« Prev 1 6 7 8 9 10 31 Next »
Advertisements