Found 10784 Articles for Python

Regex in Python to put spaces between words starting with capital letters

Samual Sam
Updated on 20-Jun-2020 08:43:36

655 Views

The problem we are trying to solve here is to convert CamelCase to separate out words. We can solve this directly using regexes by finding all occurrences of a capital letter in the given string and put a space before it. We can use the sub method from the re module.For example, for the input string −AReallyLongVariableNameInJavaWe should get the output −A Really Long Variable Name In JavaWe can use "[A-Z]" regex to find all uppercase letters, then replace them with space and that letter again. We can implement it using the re package as follows −Example Live Demoimport re ... Read More

Python Regex to extract maximum numeric value from a string

karthikeya Boyini
Updated on 20-Jun-2020 08:21:49

343 Views

The easiest way to extract the maximum numeric value from a string using regex is to −Use the regex module to extract all the numbers from a stringFind the max from these numbersFor example, for the input string −There are 121005 people in this city, 1587469 in the neighboring city and 18775994 in a far-off city.We should get the output −18775994We can use "\d+" regex to find all numbers in a string as \d signifies a digit and the plus sign finds the longest string of continuous digits. We can implement it using the re package as follows −import re ... Read More

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

Samual Sam
Updated on 20-Jun-2020 08:22:04

75 Views

We need to find the regex pattern 10+1 in a given string. For this, we can use the re module available in python. This package has a method called find all that accepts the regex and the string we want to search in. It gives us all the occurrences of the pattern in that string. For example,For the input string −10000001 hello world 10011 test100000001test.We should get the output −10000001 1001 100000001We can implement it using the re package as follows −import re occ = re.findall("10+1", "10000001 hello world 10011 test100000001test.") for i in occ: print(i)This will give the output −10000001 1001 100000001

How to replace values of a Python dictionary?

George John
Updated on 15-Jun-2020 08:27:55

9K+ Views

You can assign a dictionary value to a variable in Python using the access operator []. For example,Examplemy_dict = {    'foo': 42,    'bar': 12.5 } new_var = my_dict['foo'] print(new_var)OutputThis will give the output −42This syntax can also be used to reassign the value associated with this key. For example,Examplemy_dict  =  {    'foo': 42,    'bar': 12.5 } my_dict['foo']  =  "Hello" print(my_dict['foo'])OutputThis will give the output −Hello

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

Vikram Chiluka
Updated on 27-Oct-2022 12:12:50

2K+ Views

In this article, we will show you how to remove the same element in the list in python. In simple words removing common elements from both lists. Below are the various methods to accomplish this task − Using remove() function Using List Comprehension Using Set difference operator Using Set difference() function Assume we have taken two lists containing some elements. We will now remove the same or common elements from both lists. Using remove() function Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to store the inputList_1. ... Read More

Can someone help me fix this Python Program?

Arnab Chakraborty
Updated on 24-Jun-2020 07:26:01

61 Views

The first problem u are getting in the bold portion is due to non-indent block, put one indentation there.second problem is name variable is not definedfollowing is the corrected one -print ("Come-on in. Need help with any bags?") bag=input ('(1) Yes please  (2) Nah, thanks   (3) Ill get em later  TYPE THE NUMBER ONLY') if bag == ('1'): print ("Ok, ill be right there!") if bag == ('2'): print ("Okee, see ya inside. Heh, how rude of me? I'm Daniel by the way, ya?") name="Daniel" print (name + ": Um, Names " + name) print ("Dan: K, nice too ... Read More

Reply to user text using Python

Arnab Chakraborty
Updated on 16-Jun-2020 08:28:13

2K+ Views

You can solve this problem by using if-elif-else statements. And to make it like, it will ask for a valid option until the given option is on the list, we can use while loops. When the option is valid, then break the loop, otherwise, it will ask for the input repeatedly.You should take the input as an integer, for that you need to typecast the input to an integer using int() method.ExamplePlease check the code to follow the given points.print("Come-on in. Need help with any bags?") while True: # loop is used to take option until it is not valid. ... Read More

Python - How to convert this while loop to for loop?

Pythonista
Updated on 20-Jun-2020 07:41:33

1K+ Views

Usin count() function in itertools module gives an iterator of evenly spaced values. The function takes two parameters. start is by default 0 and step is by default 1. Using defaults will generate infinite iterator. Use break to terminate loop.import itertools percentNumbers = [ ] finish = "n" num = "0" for x in itertools.count() :     num = input("enter the mark : ")     num = float(num)     percentNumbers.append(num)     finish = input("stop? (y/n) ")     if finish=='y':break print(percentNumbers)Sample output of the above scriptenter the mark : 11 stop? (y/n) enter the mark : 22 stop? (y/n) enter the mark : 33 stop? (y/n) y [11.0, 22.0, 33.0]

How can I eliminate numbers in a string in Python?

Arjun Thakur
Updated on 05-Mar-2020 11:25:01

122 Views

You can create an array to keep track of all non digit characters in a string. Then finally join this array using "".join method. examplemy_str = 'qwerty123asdf32' non_digits = [] for c in my_str:    if not c.isdigit():       non_digits.append(c) result = ''.join(non_digits) print(result)OutputThis will give the outputqwertyasdfexampleYou can also achieve this using a python list comprehension in a single line. my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)OutputThis will give the outputqwertyasdf

How to find keith numbers using Python?

Priya Pallavi
Updated on 05-Mar-2020 11:23:28

487 Views

You can use the following code to find if a number is a keith number in python −Exampledef is_keith_number(n):    # Find sum of digits by first getting an array of all digits then adding them    c = str(n)    a = list(map(int, c))    b = sum(a)    # Now check if the number is a keith number    # For example, 14 is a keith number because:    # 1+4 = 5    # 4+5 = 9    # 5+9 = 14    while b < n:       a = a[1:] + [b]       b = sum(a)    return (b == n) & (len(c) > 1) print(is_keith_number(14))OutputThis will give the output −True

Advertisements