Found 33676 Articles for Programming

Python Program to open a file in read-write mode with truncating file

Rohan Singh
Updated on 17-Apr-2023 11:06:10

2K+ Views

In Python, we can open a file in read-write mode by truncating the file by opening the file in w+ mode. Truncating a file refers to deleting the existing content of the file before opening the file. In this article, we will discuss how we can open the file in the read-write mode by truncating the file. What is the w+ mode The w+ mode in Python is used to open the file in read-write mode with file truncation. When the file is opened in w+ mode it allows us to read and write data in the file. If the ... Read More

Python Program to Lookup enum by String value

Rohan Singh
Updated on 17-Apr-2023 11:05:31

10K+ Views

Enum in Python is a user-defined data type consisting of a set of named values. A finite set values of is defined using enums and these values can be accessed in Python using their names instead of their integer values. Enum makes the code more readable, and more maintainable and also enforces type safety. In this article, we will see how we can look up enums by their string value in Python. To look up an enum by string value we need to follow the following steps: Import enum module in the code Define enum with desired set of ... Read More

Python Program to Insert a string into another string

Rohan Singh
Updated on 17-Apr-2023 11:02:06

11K+ Views

In Python, we can insert a string into another string using the string concatenation method, string interpolation method, and using replace() method. Python provides various ways to insert a string into another string. In this article, we will understand all the methods with the help of suitable examples. Method 1: Using the Concatenation Operator String concatenation simply joins two strings together using the (+) operator. Any number of strings can be concatenated using this method in a simple and effective way. Example In the below example, we initialized three strings namely string1, string2, and inserted_string. We need to insert the ... Read More

Python Program to implement switch statement on String

Rohan Singh
Updated on 17-Apr-2023 10:56:58

3K+ Views

In Python, we can implement switch statements on a string using the dictionary-based approach, class-based approach, and lambda-based approach. Unlike other programming languages like Java, c++, etc python does not have an inbuilt switch statement. In this article, we will see how we can achieve the switch statement functionality in Python using a dictionary-based approach and class-based approach, and lambda-based approach. The Switch Statement in Other Programming Languages Before understanding how Python implements switch statements we need to understand how to switch statements work and how they are implemented in other programming languages. A switch statement is a conditional statement ... Read More

Python Program to get the index of the substring in a string

Rohan Singh
Updated on 17-Apr-2023 10:54:19

2K+ Views

In Python we can use the find() and index() methods to get the index of the first occurrence of the substring in a string. Python provides various string manipulation functions to modify and access text data. In this article, we will write a program to get the index of the substring in a string. Method 1: Using the find() method The find method searches for the specific substring which is passed as a parameter to the function and returns the starting index of the substring. If the substring is not found in the string then the find() method returns -1. ... Read More

Python program to get characters from the string using the index

Rohan Singh
Updated on 17-Apr-2023 10:50:38

759 Views

In Python, we can use various methods like using square brackets, slicing, and using indices to get characters from the string with the help of their index. A string is a sequence of characters with each character being assigned a unique index. The index specifies the position of the character in the string. The index of the first character starts with 0 and the index of the last character in the string is the length of the string minus one. We can use the index value of the character to access a particular character in the string. Accessing Characters From ... Read More

Python Program to Get a Character From the Given String

Rohan Singh
Updated on 17-Apr-2023 10:49:08

408 Views

In Python we can get a character from the given string using the indexing operator ‘[ ]’, using slices, and using indices separated by a colon. By passing the index of the character we want to access to the index operator we can easily get the character from the string. In this article, we will see how we can access the character of the string using the index operator. Using [ ] Operator Syntax string[index] Here string is the given string from which we want to access a specific character. The index is the index of the character in ... Read More

Python Program To Find all the Subsets of a String

Rohan Singh
Updated on 17-Apr-2023 10:46:28

2K+ Views

In Python, a subset of a string is a sequence of characters that is part of the original string. We can find all the subsets of a string using itertools module in Python. In this article, we will see how we can generate all the subsets of a string by making all possible combinations of characters in the string. Syntax itertools.combination(string, r) The combination() function of itertools module takes the string and r which represents the size of different combinations of strings that are possible.It returns all the combinations of characters of the string that are possible. Algorithm ... Read More

Python Program to divide a string in 'N' equal parts

Rohan Singh
Updated on 17-Apr-2023 10:44:40

5K+ Views

In Python, a string can be divided into N equal parts using the slicing method. Substrings of equal length can be extracted using the Python slicing method by specifying the starting and ending index of the substring. In his article, we will see how we can divide a string into N equal parts using the Python slicing method. To divide a string into N equal parts we need to create a function that takes the original string and the number of parts in which the string is to be divided as input and returns the resultant N equal strings.If the ... Read More

Python Program to Display all the directories in a directory

Rohan Singh
Updated on 17-Apr-2023 10:41:28

409 Views

In Python, we can use the pathlib module, os module, and glob module to display all the folders in a directory. The os module contains various functions like os.scandir, os.walk , os.listdir or glob(), and iglob() methods, etc to list all the directories in a directory. A directory is a folder in a file system that stores various files or more folders. Method 1: Using the Pathlib Module We can use the Path.iterdir() function to get the path object of the contents in the directory. We can then iterate over the path objects and filter out the directories using the path.is_dir() ... Read More

Advertisements