Found 10400 Articles for Python

Python program to move spaces to front of string in single traversal

Samual Sam
Updated on 23-Jun-2020 16:09:23

473 Views

Given a string that has set of words and spaces, our task is to move all spaces to front of string, by traversing the string only once. We will solve this problem quickly in Python using List Comprehension.ExampleInput: string = "python program" Output: string= “ pythonprogram"AlgorithmStep1: input a string with word and space. Step2: Traverse the input string and using list comprehension create a string without any space. Step 3: Then calculate a number of spaces. Step 4: Next create a final string with spaces. Step 5: Then concatenate string having no spaces. Step 6: Display string.Example Code# Function to ... Read More

Program to check if a number is Positive, Negative, Odd, Even, Zero?

Samual Sam
Updated on 17-Jun-2025 15:57:24

7K+ Views

In this article, we are going to learn how to check if the number is positive, negative, odd, even, or zero. Identifying and categorizing a number based on its characteristics is a basic operation. A number can fall into multiple categories: Positive: A number is greater than 0. Negative: A number less than 0. Zero: The number that is equal to 0. Even: The number that is divisible by 2. Odd: The number that is not divisible by 2. Let's dive into the ... Read More

Python program to print all sublists of a list.

Yaswanth Varma
Updated on 17-Jun-2025 17:27:58

5K+ Views

Printing sublists in PythonThe sublist is a portion of the list that contains one or more elements from the original list. These sublists can be contiguous(elements appear in the same order) or non-contiguous(where elements are taken from different positions). Example: Printing all Contiguous SublistsLet's look at the following example, where we are going to print all the contiguous sublists of the given list using nested loops. def demo(a): for i in range(len(a)): for j in range(i, len(a)): print(a[i:j+1]) x ... Read More

Concatenated string with uncommon characters in Python?

Yaswanth Varma
Updated on 20-Jun-2025 16:37:14

908 Views

In Python, Strings are one of the commonly used data types that are used to store sequences of characters. In this article, we are going to learn to concatenate strings with uncommon characters in Python. The uncommon characters are the characters that appear in one string but not in the other string. For example, if str1="ABC" and str2="BCD", the uncommon characters are 'A' and 'D', and the concatenated result is "AD". Using set.symmetric_difference() Method The python set.symmetric_difference() method is used to return the new set that contains the elements that are in either of the sets, but ... Read More

Python program to print all the common elements of two lists.

Chandu yadav
Updated on 30-Jul-2019 22:30:23

1K+ Views

Given two lists, print all the common element of two lists. Examples − Input : L1 = [5, 6, 7, 8, 9] L2 = [5, 13, 34, 22, 90] Output : {5} Explanation The common elements of both the list is 5. Algorithm Step1 : create two user input lists. Step2 : Convert the lists to sets and then print set1&set2. Step3 : set1 and set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Example Code # Python ... Read More

Python program to check if both halves of the string have same set of characters.

karthikeya Boyini
Updated on 23-Jun-2020 16:01:38

237 Views

Given a string, our task is to check if both halves of the string have the same set of characters or not. To solve this problem we first split the string from the middle, so we get two halves, now we check each halves having the same set of characters or not. If the length of the string is not even then ignore the middle element and check for the rest.AlgorithmStep 1: Given a string. Step 2: Break the input string into two parts. Step 3: Then convert both parts into a dictionary using Counter(iterator) method and each dictionary contains ... Read More

Python Program to calculate n+nm+nmm.......+n(m times).

Samual Sam
Updated on 23-Jun-2020 16:03:14

505 Views

Here n is given value which is positive number m is the number of times till which the series run. Our task is to calculate this series.AlgorithmStep 1: Input n, m; Step 2: Converting the number to string. Step 3: Initializing result as number and string. Step 4: Adding remaining terms. Step 5: Concatenating the string making n, nn, nnn... Step 6: Before adding converting back to integer. Step 7: return sum.Example Code# Python program to sum the series def sumofseries(n, m):    str1 = str(n)    sum1 = n    sumofstr1 = str(n)    for i in range(1, m): ... Read More

Python program to display Astrological sign or Zodiac sign for a given data of birth.

Akshitha Mote
Updated on 22-Jan-2025 13:47:27

4K+ Views

In this article, let's try to find the user's astrological sign for a given data of birth. The user input data will be compared with predefined data ranges for each zodiac sign using programming logic. For example, we can infer that the user is a Taurus if their birthdate falls between April 20 and May 20. The 12 astrological sun signs, also known as zodiac signs, are based on specific periods throughout the year. Each sign corresponds to a particular range of dates, representing distinct personality traits, characteristics, and influences. These sun signs are − ... Read More

Python program to create 3D list.

Yaswanth Varma
Updated on 17-Jun-2025 16:04:10

2K+ Views

In Python, A 3D list is also called a three-dimensional array (list of lists of lists). It can be visualized as a cube or a set of tables stacked together. It is commonly used to represent data with three indices. For example, a matrix of images (height, width, depth). In this article, we are going to learn how to create a 3D list. As Python does not have built-in support for multi-dimensional arrays like other programming languages, but using the nested lists and loops, we can create and manipulate 3D lists. Creating a 3D List In the following example, ... Read More

Python program to remove all duplicates word from a given sentence.

Yaswanth Varma
Updated on 17-Jun-2025 15:58:09

581 Views

In this article, we are going to learn how to remove all the duplicate words from the given sentence. Generally, we will encounter situations where the sentence contains the same word repeated multiple times. These duplicate words make the text look messy and can affect further processing. Removing duplicates will help to improve the readability and ensure each word appears only once in the final sentence. Using Python split() Method The Python split() method is used to split all the words in the string using the specified separator. The separator can be a comma, full-stop, or any ... Read More

Advertisements