-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

Image

  • 1
    Copy and paste your own code as well as the exact error message here. Homework is fine, but you have to show us your effort – shree.pat18 May 08 '22 at 08:36

2 Answers2

1
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)

codester_09
  • 5,622
  • 2
  • 5
  • 27
0

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))
IsaacMak
  • 97
  • 1
  • 10