Python Articles

Page 35 of 852

How to eliminate repeated lines in a python function?

Sarika Singh
Sarika Singh
Updated on 09-May-2025 6K+ Views

In this article, we will discuss how to delete multiple lines that are repeated in a Python Function. If the file containing the program is small and only has a few lines, we can remove repeated lines manually. However, when dealing with huge files, we need a Python program to do so. Using the File Handling Methods Python has built-in methods for creating, opening, and closing files, which makes handling files easier. Using the methods, we can also perform several file actions, such as reading, writing, and appending data (while files are open). To remove duplicate lines from a text ...

Read More

Find all the patterns of \\\"10+1\\\" in a given string using Python Regex

Sumana Challa
Sumana Challa
Updated on 09-May-2025 275 Views

The term "10plus1" is a specific pattern in a binary string that starts with a digit '1' followed by at least one or more '0' and ending with a '1'. In regular expressions this pattern is represented as - 10+ 1 Using re.findall() The re.findall() method accepts a pattern and a string as parameters, finds the given pattern in the entire string, and returns all the matches in the form of a list.To find the patterns of "10+1" in a given string, we just need to pass the same as pattern to the findall() method along with the string.Example ...

Read More

How to bitwise XOR of hex numbers in Python?

Sumana Challa
Sumana Challa
Updated on 09-May-2025 4K+ Views

The bitwise XOR is a binary operation in which we compare two binary numbers bit by bit and return the value "1" if the bits are not same, and 0 if they are the same. The XOR(exclusive OR) operation follows the below rules - A B A ⊕ B ...

Read More

How can I remove the same element in the list by Python

Sumana Challa
Sumana Challa
Updated on 09-May-2025 3K+ Views

A list is a built-in Python data structure that is used to store an ordered collection of items of different data types. It often occurs that lists contain duplicate values, i.e., the same element repeating multiple times, which causes data inaccuracies. In this article, we will discuss the approaches that can be used to remove the repeated elements from a list. Using set() Using List Comprehension Using a For Loop Using Dictionary fromkeys() Using set() Function The set() function accepts an iterable ...

Read More

How to find the longest common substring from more than two strings in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 08-May-2025 2K+ Views

The substrings are the sequence of characters that appear within the string. While working with multiple strings, it is useful to find the longest common substring. There are various ways to find the longest common substring from more than two strings. one of the most common approach is using the dynamic programming. Let's dive into the article to learn more about this task. Using Dynamic Programming In this approach, we are going to use the dynamic programming (DP), Here we compare the strings pairwise (starting with first two strings), we find their longest common substring using a DP ...

Read More

What are character classes or character sets used in Python regular expression?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 07-May-2025 2K+ Views

In this chapter, we will understand character classes, how they work. and provide simple examples and programs to explain their usage. One of the main components of regular expressions is character classes or character sets. Character classes help you to define a set of characters that will match in a string. They can be used to define a range of characters or specific characters to consider during a search. Character Classes or Character Sets A "character class, " also known as a "character set, " which helps you inform the regex engine to match only one of numerous characters. Simply ...

Read More

How to do string concatenation without \\\\\'+\\\\\' operator in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 07-May-2025 3K+ Views

In this article, we are going to find out how to do string concatenation without the plus operator in Python. Generally, String concatenation is the process of joining two or more strings into one, and the common way to do this is by using the '+' operator. For example, writing "Welcome" + " " + "TP" results in the string "Welcome TP". But here we need to perform the concatenation without the '+' operator. For achieving this, Python provides alternative methods, which we will explore one by one. Using Python join() Method The first approach is ...

Read More

How to extract file extension using Python?

Sarika Singh
Sarika Singh
Updated on 07-May-2025 6K+ Views

In a few scenarios, we need to extract the extension of a file to perform specific operations based on its type, such as validating image formats or filtering document files. Python provides different ways to achieve this using the os and pathlib modules. In this article, we'll explore how to get a file’s extension with different approaches.  Using os.path.splitext() The OS file path manipulation is made simple with the help of the Python os.path module. It provides methods to perform operations to receive data from file paths, opening, saving, and updating. The os.path.splitext() method of the os module in Python ...

Read More

How to pass a json object as a parameter to a python function?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 07-May-2025 11K+ Views

We can pass a JSON object as a parameter to a Python function using the json.loads() method. we can also convert the JSON string into a Python dictionary or list, which depends on its structure. Once converted, the data can be used just like any other dictionary or list in Python. JSON Object Consider a JSON object to pass to a python function. We will use it further in this article - { "name":"Rupert", "age": 25, "desig":"developer" } Using json.loads() Function Before passing a JSON object as a parameter ...

Read More

How to expand tabs in string to multiple spaces in Python?

Sarika Singh
Sarika Singh
Updated on 07-May-2025 3K+ Views

In Python, handling white spaces between strings is easy. Sometimes, we may want to add space in a string, but we are not sure exactly how much. Python provides different ways to manage this, and one useful method is expandtabs() method. Using the expandtabs() Method The expandtabs() method in Python is used to replace tab characters (\t) in a string with spaces. It returns a new string where each \t is replaced with the number of spaces needed to reach the next tab stop. You can control how many spaces are used by passing a tabsize ...

Read More
Showing 341–350 of 8,519 articles
« Prev 1 33 34 35 36 37 852 Next »
Advertisements