Home > Mobile >  New to Python(3), may I ask for some help on my homework?
New to Python(3), may I ask for some help on my homework?

Time:05-08

I am very new to programming & currently trying to learn python 3. My homework asks to create a variable that contains a string. The string is to contain a few phrases, line breaks, and other variables that contain strings and an integer.

So far I am running into a few errors, namely the "unexpected character after line cont." and the one that says the string can't "process" (I forgot the exact wording) the integer...

below is what I have so far.

the assignment asks for me to contain a string, that when printed, displays text formatted such as below;

First Name: XXXX Last Name: XXXX Student ID: XXXX

If you can provide any insight I would be so grateful!! Thank you

enter image description here

CodePudding user response:

first = 'Robo'

last = 'Angel'
sid = 12345


msg = "First Name: "   first   "\nLast Name: "   last   "\nStudent ID: "   str(sid)

print(msg)
# OR #

msg = f"First Name: {first}\nLast Name: {last}\nStudent ID: {sid}"

print(msg)

CodePudding user response:

alright, first you have to define some variables like:

First_Name = "abc"
Last_Name = "def"
Student_Id = 1234

And then you have to print the formatted string, There few way can do it.
1. string concatenation

print("First Name: " First_Name " Last Name: " Last_Name " Student ID: " Student_Id)

I am won’t recommend this way(string string).

2. “.format()”

print("First Name: {} Last Name: {} Student ID: {}".format(First_Name,Last_Name,Student_Id)

3. f-string


print(f"First Name: {First_Name} Last Name: {Last_Name} Student ID: {Student_Id}")

i will recommend this (f-string)

4. Modulo String

print("First Name: %s Last Name: %s Student ID: %d" % (First_Name, Last_Name, Student_Id))
  • Related