Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'd like to make a program in python that analyzes a users input.
For example if the user writes "Hello, I'm 19 years old", the program can see that
the string has numbers in it and assigns "19" to a variable.
It could also at the same time see that the string has the word "years" in it and makes the variable:
years = 19
I'm pretty new to python and programming in general, so sorry if the question is weird.
Thanks.
You can count the length of the string using function len. str can contain all chars and letters. Try the following code:
word = input("Waiting for input: ")
print("Found "+str(len(word))+" letters!")
Testing it:
Waiting for input: hello
Found 5 letters!
Testing it with numbers and other chars:
Waiting for input: hello123!##
Found 11 letters!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
I want to replace a word in a string which is determined by the word entered by the user.
For example, if user enters "pot", the code replaces it with "who" but if he enters "top" then it becomes "mam".
I have a set of 225 words and their replacements. I am using python for this. Replacing each word individually in a condition is really impractical. is their a quicker way to do this. I currently have no code as I am totally confused how to do this.
Using excel throws an error no matter what.
Yes, there is a very convenient way to do this; python dicts:
d = {"pot": "who", "top": "mam"}
print(d["pot"])
print(d["top"])
Output:
who
mam
In Python 3.1, you can use the new match - case statement:
s = "pot"
match s:
case "pot":
print("who")
case "top":
print("top")
case _:
print(None)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So my problem is that I need to create a simple python program that takes an input of 6 characters from the user as a postal code and takes the first character and matches it to the corresponding province. For example: Alberta postal codes start with a T so i started off my program with:
pCode = {"Alberta": "T"}
a = input("Enter a 6 character postal code: ")
just to start off the dictionary and get an input. The instructions are also that I must not use loops, lists or if statements. How would i go about doing this?
Your dict is the wrong way round. The thing you want to look up (key) goes first in each pair
pCode = {"T": "Alberta"}
a = input("Enter a 6 character postal code: ")
You can get the first character of the post code as a[0]
print(pCode.get(a[0]))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a bible verse look up. The idea is to use bible gateway website and use python to replace numbers in the url automatically to send straight to the website page. So, if the url is: http://www.biblegateway.com/passage/?search=|John|+|4|%3A|1||6|&version=ESV
How can I make the numbers and name in between the "| |" variables so that it can take user input?
Sounds like you are asking about string formatting, python 2 example:
>>> url = "http://www.biblegateway.com/passage/?search=|John|+|4|%3A|1||{number}|&version=ESV"
>>> number = raw_input('Enter number: ')
Enter number: 10
>>> url.format(number=number)
'http://www.biblegateway.com/passage/?search=|John|+|4|%3A|1||10|&version=ESV'
Pay attention to the {number} placeholder in the url.
Note that if you are on python3, it would be input() instead of raw_input().