Python Checking usernames with regular expressions [closed] - python

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 2 years ago.
Improve this question
I have a task i need some help with. The task is to create a python script that asks the user to enter a desired username. The username has to be as following: "a11aaaaa".
So starting with a letter, 2x numbers, 5x letters. This is the rule for how the username should look and if the given input does not match that, the user shall be able to try again until getting it right.
Thankful for any help!

As I recently learnt from another user on SO, \w includes \d. Therefore, '^\w\d{2}\w{5}$', as suggested by some users here, will match, for example, 12345678.
To fix that, just specify the character class explicitly:
import re
regex = re.compile('^[A-Za-z]\d{2}[A-Za-z]{5}$')
while True:
password = input('Please enter a password: ')
if regex.search(password):
print('Yay! Your password is valid!')
break
else:
print("Oh no, that's not right. You need a letter, then two numbers, then five letters. ", end='')

^\w{2}\d{2}\w{5}$
You can use this handy site to experiment with regex: https://regex101.com/

You can do something like that:
import re
while True:
name = input('Enter your name')
if re.match('^\w\d{2}\w{5}$', name):
break

Try this one:
import re
uname = input("Enter your username: ")
regex = re.compile(r"^[A-Za-z]{1}\d{2}[A-Za-z]{5}$")
if regex.findall(uname):
print ("Valid username")
else:
print ("Invalid username")

Related

Input function doesn't read the next line of code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to learn how the input function works but for some reason it doesn't want to read any code that's after it.
Here's my code:
f_name = input("enter name: ")
print("welcome", f_name)
and this is the result:
enter name: This is my name
and nothing else comes after I hit enter.
For python 3.x compiler, this works fine. I guess what you can do is, print your data in one of the following ways, and see, if that works for you.
Taking f_name as UserName
1. Using f-String
>>> print(f"Welcome {f_name}.")
Welcome UserName.
2. String concatenation
>>> print('Welcome ' + f_name)
Welcome UserName
3. Using old school % formatting
>>> print('Welcome %s.' % f_name)
Welcome UserName.
4. Using .format()
>>> print('Welcome {}.'.format(f_name))
Welcome UserName.
Using these you can get the output. The code which you have pointed works out for me, although the above solutions is what gives the output surely. Give it a go, and let us know. :)
if it automatically closes, because nothing to execute after that so it closes try this to see the output
f_name = input("enter name: ")
print("welcome", f_name)
input()
For some reason running it with the default Python editor worked for me.
Sublime Text Doesn't work with input for some reason nor running it with cmd worked too.

It's a question from me as I am a beginner in programming ? I want to ask a simple question related to Python [closed]

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 2 years ago.
Improve this question
The question is, i executed a program which told me to input a name. Now whenever I input a number as the input instead of letters or a string. The number gets taken as my name. I want the program to tell me to enter a name and not a number whenever I type in a number as my name instead of a string. Can you please help me with this problem in Python. I want a simple code and should not contain any import functions as I don't know about it ! Only simple begineers code please.
Here's my code:
name = input("Enter a name : ")
print("Hello", name)
Whenever I enter a number as input for example 5, then it prints Hello 5 but I want it to print Please Enter A Valid Name whenever I input a number. Please Help !
One very simple solution is to use the built-in function isnumeric(), which returns true if all characters in the string are numeric.
name = input("Enter a name : ")
if name.isnumeric():
print("That's not a name!")
else:
print("Hello", name)
Another approach is to use isalpha(), which returns true if all characters in the string are letters.
name = input("Enter a name : ")
if name.isalpha():
print("Hello", name)
else:
print("That's not a name!)
Be careful though, as this will reject some strings that are still names, such as "John Smith" or "Jean-Pierre"
Once you have a string, you can use regular expressions to find out if it contains undesirable characters, as per the following transcript:
>>> import re
>>> name = input("Name? ")
Name? Pax
>>> if re.search("[0-9]", name):
... print("Has digits")
...
>>> name = input("Name? ")
Name? Bobby27
>>> if re.search("[0-9]", name):
... print("Has digits")
...
Has digits
This particular case checks whether any digits exist in the string but you can make the regular expression arbitrarily complex if you want more targeted checks to be done.

Where would I put the .title function in order for the final output to have the words beginning with a capital letter? [closed]

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 4 years ago.
Improve this question
I know this is probably a simple problem but as a beginner I thought I would ask. I have created a simple Python script to run through terminal where, after being asked three questions, the user will be given an output with it all in. I would like to add to this by capitalising all words answered and I know I could use the .title function but im not sure where to put it. any help would be much appreciated.
#ask user for age
name = input('What is your name?: ')
print(name)
#ask user age
age = input('How old are you?: ')
print(age)
#ask user for city
city = input('What city were you born in?: ')
print(city)
#ask user what they enjoy
hobby = input('What do you enjoy doing in your spare time?: ')
print(hobby)
#create output text
string = 'Your name is {} and you are {} years old. you were born in {} and you enjoy {}'
output = string.format(name, age, city, hobby)
#print output to screen
print(output)
You will put it in this line:
output = string.format(name.title(), age.title(), city.title(), hobby.title())
This is pretty straight forward since this is what you add to the final output.
You could also add it straight after asking for input like this:
my_name = input('Enter your name:').title()

How do functions work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Except for in maths examples, of which there are many, I don't understand functions properly.
What is wrong with this?
def get_address():
address = raw_input ("What is your address: ")
return address
I'm not able to get a variable address returned for use later.
What you did, you failed to call the function that is why you facing this problem.
Use this instead and you can able to get the address of a function.
In Python v2.x
#!/usr/bin/python
def get_address():
address = raw_input("What is your address: ")
return address
a = get_address()
print a
What is raw_input?
It ask the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string.
In Python v3.x:
#!/usr/bin/python
name=input('Enter your name : ')
print ("Welcome %s, Let us be friends!" % name);

How do you save the inputs into text file? [closed]

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 6 years ago.
Improve this question
How do i save the inputs of program into a file from the program.
Python only:
i need to add it to this:
TASK 2 AGAIN
sentence = input("Please input a sentence: ")
print(sentence)
word=input("Please enter word and position wil be shown: ")
if word in sentence:
print("Found word")
else:
print ("Word not found")
But i haven't got a clue
I assume this is what you're asking for
text_file = open("Output.txt", "w")
text_file.write(stuff)
text_file.close()
Your question seems to have two main parts to it: how do I get inputs in Python and how do I save data to a file in Python.
To get input from the terminal:
>>> data = input('Input: ')
>>> Input: hello, world!
To save to a file:
>>> with open('output.txt', 'w') as f:
>>> f.write(data)
You can find more information about inputs here and file i/o here
Edit 0: #Gazzer, if you want to save sentence + input you'll need to do f.write(sentence + input) rather than using .save().
Edit 1: #Gazzer, I got something like the below to work (note: the code does not show position of the found word):
sentence = input("Please input a sentence: ")
word = input("Please enter word and position wil be shown: ")
with open('output.txt', 'w') as f:
f.write('{} {}'.format(sentence, word))
If you run into more issues, there are hundreds of resources all over the web to ask for help. Stack Overflow, learnprogramming, and many more.
Next time you ask a question, it is really helpful for those answering if you provide a code snippet you are working on and what the problem/errors are.

Categories