Programming Articles

Page 139 of 2547

Python Program to Get a Character From the Given String

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 506 Views

In Python, you can access individual characters from a string using indexing with the [ ] operator. Python uses zero-based indexing, where the first character is at index 0. You can also use negative indexing to access characters from the end, and slicing to extract multiple characters. Using [ ] Operator Syntax string[index] Here string is the given string from which you want to access a specific character, and index is the position of the character (starting from 0). Positive Indexing Access characters using positive indices starting from 0 − ...

Read More

Python Program To Find all the Subsets of a String

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 2K+ Views

In Python, a subset of a string is a sequence of characters that appears in the original string. We can find all the subsets of a string using the itertools module to generate combinations of characters. In this article, we will see how to generate all possible subsets by creating combinations of different lengths. Syntax itertools.combinations(iterable, r) The combinations() function takes an iterable (like a string) and r which represents the length of combinations to generate. It returns all possible combinations of that specific length. Algorithm Initialize an empty list to store ...

Read More

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

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 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 this 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. ...

Read More

Python Program to Display all the directories in a directory

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 545 Views

In Python, we can use the pathlib module, os module, and glob module to display all the directories within a directory. The os module contains various functions like os.scandir(), os.walk(), os.listdir(), and glob methods like glob() and iglob() to list all directories in a specified path. Method 1: Using the Pathlib Module The pathlib module provides an object-oriented approach to handle file system paths. We can use Path.iterdir() to get path objects of directory contents and filter directories using is_dir(). Syntax Path('directory_path').iterdir() Example This example shows how to list all directories in ...

Read More

Solving the First Law of Thermodynamics using Python

Dr Pankaj Dumka
Dr Pankaj Dumka
Updated on 27-Mar-2026 2K+ Views

The first law of thermodynamics is related to energy conservation − if energy in one form disappears then the energy will appear in some other form. In thermodynamics we are primarily concerned about heat, work and internal energy. Heat and work are forms of energy called "energy in transit", making them path functions that cannot be stored by a system, whereas internal energy is a property of the system that can be stored. For a closed system, the first law is written as − $$\mathrm{\Sigma Q=\Sigma W}$$ This is only valid for a closed system undergoing a ...

Read More

Python Program to Differentiate String == operator and__eq__() method

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 398 Views

In Python, the comparison operator == and __eq__() method are closely related but serve different purposes when working with strings and objects. The == operator provides default comparison behavior, while __eq__() allows custom equality logic. Understanding their differences is crucial for effective string comparison in data analysis and object-oriented programming. == Operator in Python The == operator is used to compare two values for equality. It returns True when values are equal and False when they differ. For strings, it compares content regardless of memory location ? str1 = "Hello World" str2 = "Hello World" str3 ...

Read More

Python Program to demonstrate the string interpolation

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 324 Views

In Python, we can demonstrate string interpolation using f-strings, the % operator, and the format() method. String interpolation is the process of inserting dynamic data or variables into a string, making it useful for creating formatted strings without manual concatenation. Method 1: Using f-strings An f-string is a string literal that starts with f or F. The prefix indicates that the string contains expressions enclosed in curly braces {}, which are evaluated at runtime. Example Here we create variables and use an f-string to interpolate their values into a formatted message ? name = ...

Read More

Python Program to create a String object

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 1K+ Views

In Python, we can create a string object using Python's built-in function str() and also by assigning a sequence of characters to a variable. The sequence of characters is enclosed in single quotes, double quotes, or triple quotes for multiline strings. In this article, we look at the various ways to create string objects in Python. Using Single Quotes We can create a string object by simply assigning a sequence of characters enclosed in single quotes to a variable − my_string = 'Hello World!' print(my_string) print(type(my_string)) The output of the above code is − ...

Read More

Python Program to compare two strings by ignoring case

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 11K+ Views

In Python, we can compare two strings while ignoring their case using several approaches. Strings are character sequences that can contain uppercase and lowercase letters. When comparing strings case-insensitively, we need to normalize both strings to the same case before comparison. Using lower() Method The most common approach is converting both strings to lowercase using the lower() method before comparison ? string1 = "Hello" string2 = "hello" if string1.lower() == string2.lower(): print("The strings are equal, ignoring case.") else: print("The strings are not equal, ignoring case.") ...

Read More

Python Program to Clear the String Buffer

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 4K+ Views

In Python, a string buffer is a mutable sequence of characters that can be modified before writing to an output stream. Python's StringIO class from the io module provides an in-memory buffer for string operations. We can clear this buffer using different methods depending on our requirements. Method 1: Using truncate() and seek() The truncate(0) method removes all content from the buffer starting at position 0, while seek(0) resets the cursor to the beginning ? from io import StringIO # Create a string buffer buffer = StringIO() # Add some text to the buffer ...

Read More
Showing 1381–1390 of 25,469 articles
« Prev 1 137 138 139 140 141 2547 Next »
Advertisements