'New to Python(3), may I ask for some help on my homework? [closed]
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
Solution 1:[1]
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))
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sharim Iqbal |

