Python character name [duplicate] - python

This question already has answers here:
print variable and a string in python
(6 answers)
Closed 10 months ago.
First I assigned a variable
character_name = "Patrice"
On the print function, I wrote
print( " Patrice does a wonderful job at his workplace ")
Instead of writing Patrice on the print function, I wanted to declare the variable that would print Patrice, How would I do it?

There's various ways to achive this, you might want to look at Pythons f-strings:
print(f'{character_name} does a wonderful job at his workplace')

Related

Is there a way to take an element from a list and use it in the name of a variable? [duplicate]

This question already has answers here:
Convert string to variable name in python [duplicate]
(3 answers)
Closed 2 years ago.
Say I have a list:
questions = ['a','b','c','d','e']
And I want to make a variable named after an element in this list:
questions[2] = 'Hello world'
This is the way I thought I could do it but when I try to print it:
print(b)
I get this error:
NameError: name 'b' is not defined
Obviously, python has made an entirely new variable called 'questions[2]'. How can I get python to recognize questions[2] as 'b' and not the 'questions[2]'?
Do you know the difference between b and 'b'? You are missing between variable's name and value.
While it's possible, there really shouldn't be any reason to do this. But anyways you can use exec().
questions = ['a','b','c','d','e']
exec(questions[1] + ' = "Hello world"')
print(b)
Note that using exec() is generally discouraged. Perhaps a dictionary would suit your needs instead.

EASY How do I use the count() and capitalize() function? [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I am new to Python and stack overflow. How do I use the count and capitalize functions for my strings? The app I am learning from appears to have this mixed up because it dosen't work in vscode.
It seems like the capitalize function call is ignored. What am I doing wrong? If someone could tell me how to use the count function as well I have the same problem. The app I learn from is called Programminz.
This is Python 3
practice = "CaPITalIzE mE proPerLy"
practice.capitalize()
print(practice)
string_name.capitalize()
string_name: It is the name of string of
whose first character we want
to capitalize.
Try to use :
string = "CaPITalIzE mE proPerLy"
capitalized_string = string.capitalize()
print('Old String: ', string)
print('Capitalized String:', capitalized_string)

Problem with "If"/"else" statement in python [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I am just getting started learning, and I've been trying for awhile to get this done but I don't know what I am doing wrong
name = input("Enter name :")
if name is "Uber":
print("Hello there, General "+ name ,",you are a bold one!")
else:
print("Begone, " + name , ", you do not belong here.")
I want it to say "Hello there, General Uber, you are a bold one!" if the input is Uber, otherwise it must say "Begone (name),you do not belong here."
is is used for comparing the identity of objects, and the input and "Uber" are not the same object.
To test for equality, use ==:
if name == "Uber":

How to use this code in python 3 version? [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 8 years ago.
I want to run this code in the python 3 but i can't.Whenever i try to run the code,i get the invalid syntax error.
age = 20
name = 'Swaroop'
print '{} was {} years old when he wrote this book'.format(name, age)
print 'Why is {} playing with that python?'.format(name)
Please help me.
Thank you.
Put parentheses around the print function calls.
print('{} was {} years old when he wrote this book'.format(name, age))
print('Why is {} playing with that python?'.format(name))
In Python2, print is a statement, and does not require parenthesis.
In Python3, print is a function, so it requires parentheses around its arguments.

How do you format for thousands separator in Python? [duplicate]

This question already has answers here:
Add 'decimal-mark' thousands separators to a number
(9 answers)
Closed 8 years ago.
I am simply trying to execute the following command in Python:
print("Number is", format(49000.12345,"10.2f"))
So I would like it to print out like 49,000.12 for the number.
My teacher taught us to put a comma in "10.2f" like ",10.2f" for the thousands separator but it's not working. Could someone please tell me the correct simple way similar to that?
Thank you
See this: http://www.python.org/dev/peps/pep-0378/ It is the PEP introducing the ability into Python 2.7, 3.1 and later versions.

Categories