Regarding the string format in python, what does '[%s]' mean? - python

I saw a line of code:
re.compile('[%s]' % re.escape(string.punctuation))
But I have no idea about the function of [%s]
Could anyone help me please?
Thank You!

It is a string formatting syntax (which it borrows from C).
Example:
name = input("What is your name? ")
print("Hello %s, nice to meet you!" % name)
And this is what the program will look like:
What is your name? Johnny
Hello Johnny, nice to meet you!
You can also do this with string concentation...
name = input("What is your name? ")
print("Hello + name + ", nice to meet you!")
...However, the formatting is usually easier.
You can also use this with multiple strings.
name = input("What is your name? ")
age = input("How old are you? ")
gender = ("Are you a male or a female? ")
print("Is this correct?")
print("Your name is %s, you are %s years old, and you are %s." % name, age, gender)
Output:
What is your name? Johnny
How old are you? 37
Are you a male or a female? male
Is this correct?
Your name is Johnny, you are 37 years old, and you are male.
Now, please, before asking any more questions, see if they exist!

Related

How can I print the input values in between a line?

name = input(enter name)
age = input(age)
print(“My name is print(name). I’m print(age) years old.”)
Nobbie experiment.
Beginner level task.
And the above query came to my mind.
name = input("enter name: ")
age = input("age: ")
print(f"My name is {name}. I am {age} years old")
Study and try to understand Keshav V. answer using f-strings, this is the "modern" approach and will serve you well time after after time.
A more long handed approach would be to rewrite your program like this (as a stepping stone to understanding the f-string format)..
name = input("enter name ")
age = input("age ")
print("My name is", name, "I’m", age, "years old.")
Notice that print will accept as many items as you want to print and place them on the same line.
You could also use f-string formatting once you have capture the input in name and age variable.
print(f"My name is {name}. I'm {age} years old")
My name is laura. I'm 18 years old
You can simply use python f-strings.
name = input('Enter your name ')
age = input ('Enter your age ')
print(f'My name is {name}. I\'m {age} years old.')
If you also want you can go with the format function.
or this
name = input('Enter your name ')
age = input ('Enter your age ')
print('some text' + name + 'also some text')
good luck at programming,
You can also try string formatting.
name = input('Enter your name ')
age = input ('Enter your age ')
print('My name is {}. I\'m {} years old.').format(name,age)
More About String Formatting
Would be a good idea to take some time to read a Python book.
name = input("enter name")
age = input("age")
print( f"My name is {name}. I'm {age} years old.")
You can use a formatted string for this:
def name_and_age(name, age):
return f"My name is {name} and I'am {age} years old"
print(name_and_age('Max', 35))
#output: My name is Max and I'am 35 years old

How do you print out the input you put in?

I am writing a piece of code which asks for the users first name then second name and then their age but then i print it out but I'm not sure how to please give an answer.
print ("What is your first name?"),
firstName = input()
print ("What is you last name?"),
secondName = input()
print ("How old are you?")
age = input()
print ("So, you're %r %r and you're %r years old."), firstName, secondName, age
You want to use string.format.
You use it like so:
print ("So, you're {} {} and you're {} years old.".format(firstName, secondName, age))
Or in Python 3.6 upwards, you can use the following shorthand:
print (f"So, you're {firstName} {secondName} and you're {age} years old.")
Use
print ("So, you're %r %r and you're %r years old." % (
firstName, secondName, age))
You might want to consider using %s for the strings and %d for the number, though ;-)
New style string formatting in Python:
print("So, you're {} {} and you're {} years old.".format(firstName, secondName, age))
This is the fixed code:
firstName = input("What is your first name?")
secondName = input("What is you last name?")
age = input("How old are you?")
print ("So, you're " + firstName + " " + secondName + " and you're " + age + " years old.")
It is pretty easy to understand, since this just uses concatenation.
There are two ways to format the output string:
Old way
print("So, you're %s %s and you're %d years old." % (firstName, secondName, age)
New way (Preferred way)
print("So, you're {} {} and you're {} years old.".format(firstName, secondName, age))
The new way is much more flexible and provides neat little conveniences like giving placeholders an index. You can find all the differences and advantages here : PyFormat

Basic Python function

I am trying to complete two different questions but cannot get them to work. Please help me understand where I went wrong.
1) For each number between 1 and 100, odds should be normal and even numbers should print out the word "Billy". Must start at 1 not 0 and include the number 100. Here's my answer (I know I'm way off)
for i in range(1,101):
if i % 2 == 0:
print(Billy)
else:
print(i)
2) Ask the user: "What is your name?". Response should look like "Hello Billy" for all names except Joe and Susie. For Joe it should say "Hi Joe :)" and for susie it should say "Ahoy Susie :D". Here is where I'm at:
name = input("What is your name?")
if name == "Joe":
print("Hi Joe :)")
if name == "Susie":
print("Ahoy Susie :D)
else: print("Hello", name)
try this
for i in range(1,101):
if i % 2 == 0:
print('Billy') #you missed quote marks here
else:
print(i)
(bad indentation, and missing quote marks)
and
name = input("What is your name?")
if name == "Joe":
print("Hi Joe :)")
elif name == "Susie":
print("Ahoy Susie :D") #and you missed quote marks here
else:
print("Hello" + name)
...same issues.

Python keeps saying NameError: name 'name' is not defined

def main():
name = raw_input("What is your name?")
age = raw_input("How old are you?")
color = raw_input("What is your favorite color?")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, age, color)
if __name__ == '__main__':
main()
I am trying to replicate the following code from Codeacademy:
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)
Print should be in the scope of the main function in order to acces its variables:
def main():
name = raw_input("What is your name?")
age = raw_input("How old are you?")
color = raw_input("What is your favorite color?")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, age, color)
if __name__ == '__main__':
main()
This should do.
name, age, and color are all local to main. Thus, you cannot access them outside of the function.
I think the best solution here would be to indent that print line one level:
def main():
name = raw_input("What is your name?")
age = raw_input("How old are you?")
color = raw_input("What is your favorite color?")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, age, color)
if __name__ == '__main__':
main()
Now, it is in the same scope as name, age, and color and can access them just fine.
The only time you set name is when you call the main function. The only time you call the main function is after you call the print function (because the print statement isn't part of main). Therefore, name is undefined.
If you intended to replicate the codeacademy code, you need to adjust the indentation of the print statement so that it is at the same level as the raw_input statements. This is because python uses the amount of indentation to know which block a line of code belongs to. You want the print statement to be in the same block as the input statements.
For example:
def main():
name = ...
age = ...
color = ...
print ...

Making a greeting program in python

Really easy stuff, sorry in advance for the idiotic question but I'm just not understanding how to do this... I want to make a function that asks the user's name and then greets the user by name.
so if I were to do greet(yo)...
What's your name? Tom
Yo, Tom
I've got this so far:
def greet(yo):
print("Whats your name")
raw_input(name)
return yo + name
Save the return value from raw_input into a variable:
def greet(yo):
name = raw_input("What's your name: ")
return yo + name
print greet("hi! ")
Demo:
>>> greet('hi! ')
What's your name: Tom
'hi! Tom'
For python 2, use raw_input to get input.
For python 3, use input to get input.
def greet(greeting):
name = raw_input("Hi, whats your name?") #python 2
return greeting + name
print greet("Hi, ")
# Python program that asks the user to enter their name, and then greet them.
name = input("Hello, What's your name?")
# Then type in your name.
print("Hello " + name+ " it's nice to meet you"+ "!")
For example:
Hello, What's your name?Bob
Hello Bob it's nice to met you!

Categories