pythonpython-3.xstringescapeutils

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


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


Solution

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