How do I remove all white space in any given input? [closed]
This isn’t elegant, but you could strip line endings, then replace the spaces and tabs: askuser = askuser.strip().replace(‘ ‘,”).replace(‘\t’,”)
This isn’t elegant, but you could strip line endings, then replace the spaces and tabs: askuser = askuser.strip().replace(‘ ‘,”).replace(‘\t’,”)
Besides the error with the ( going through the code some modifications could make this a little more optimal. We can use a try, except block to eliminate the ERROR else statement and handle that directly when we receive the input if is not a valid int and we force a valid int between 0 … Read more
First thing you should know is, randint(start, end) function randomly generates number starting from the start to end. So in your case, it generates a number more than what indexes you have in your lists. You can do, people = randint(0, len(persons)-1)
Python is free software, and it’s open source. Anyone can use it in a commercial or public project. Python is not a proprietary language, for the same reasons. Source: https://docs.python.org/3/license.html
Here some ideas: use new_str = str.upper() so beer and Beer will be same (if you need this) use list = str.split() to make a list of the words in your string. use set = set(list) to get rid of double words if needed. start with an empty word_list. Copy the first set in the … Read more
For this particular situation, you can use eval. >>> n = (102, ‘(24, -20)’) >>> n = list(n) >>> n [102, ‘(24, -20)’] >>> n[1] = eval(n[1]) >>> n [102, (24, -20)] >>> new = (n[0], [1][0], n[1][1]) >>> new (102, 1, -20)
Use re.finditer: import re sentence = input(“Give me a sentence “) word = input(“What word would you like to find “) for match in re.finditer(word, sentence): print (match.start(), match.end()) For word = “this” and sentence = “this is a sentence this this” this will yield the output: (0, 4) (19, 23) (24, 28)
Couple of points: Misplaced return statement. Should be at end. if yes(): It is wrong. You want to compare function input with yes. It should be if s == ‘yes’:. Same for rest also. Since you have written function definition as def shut_down(s):, it is expecting one argument. You should pass one argument while calling … Read more
Time::HiRes::time() is the equivalent. If you don’t need partial seconds, you can simply use time(), though.
The way to do this is clearly documented here. Example: >>> myString = “subString1|substring2|subString3” >>> myString = myString.split(“|”) >>> print myString [“subString1”, “subString2”, “subString3”]