View Single Post
Old 2019-11-12, 23:07   #8
Dylan14
 
Dylan14's Avatar
 
"Dylan"
Mar 2017

2·293 Posts
Default Concatenation of strings and the "input" command

In the previous section, we dealt with the print command and we also took our first serious look at strings. However, we kept it very simple, and we considered only one string at a time. Now, what if we want to have multiple strings? This is very simple: we can define two variables as strings. We can also print said strings:

Code:
string1 = "This is a string"
string2 = "I like mersenneforum"
print(string1)
print(string2)
This yields:

Code:
This is a string
I like mersenneforum
Now, suppose we want to connect the strings together, so that we have one string that contains both string1 and string2. In order to do this, we need to use string concatenation. Luckily, we can do easily in Python. If we use the plus sign between two string objects, we can "concatenate" them together.
Going back to the previous strings that we defined, we could concatenate as follows:
Code:
string1 = "This is a string"
string2 = "I like mersenneforum"
string3 = string1 + string2
print(string3)
This gives the following:
Code:
This is a stringI like mersenneforum
This leads to a thing about concatenation: it just sticks the strings together into one, without putting any white space between the two.(*)
Now, what if we have more than two strings that we want to concatenate? Easy! Just put a plus sign between each string you want to string together, like so:

Code:
print("This is a string " + "and " + "I like mersenneforum")
which yields

Code:
This is a string and I like mersenneforum
Note that I only used three strings in this example. You can expand this to N strings, where N is any positive number greater than 2. Just make sure to put a plus sign next to each pair of strings.(**)

The "input" command:

In the previous section, I used the input() command to get Python to print whatever we wanted to type. It would be wrong to just ignore what it does before we get too deep into the guide, so we will cover it here.
The input function is a simple function, which may take either 0 or 1 arguments. It basically interrupts the interpreter, requests user input, and then when the user presses Enter on the keyboard, the program will continue. The only argument that can be placed in the function is a string, which will be a prompt which will be printed when the interpreter reaches that point in the program.
The input function will encounter an error if an end of file character is received (***).
Example of input() without a prompt:

Code:
print(input())
This will print out whatever you type in.

Example of input() with a prompt:

Code:
age = input('Enter your age: ')
print("You are " + str(age) + " years old")
this prints out the user's age:

Code:
Enter your age: 23 
You are 23 years old
The EOFError (on Google Colab, after pressing Ctrl+D in input())(****):

Code:
EOFError                                  Traceback (most recent call last) <ipython-input-6-6610b0876cd9> in <module>() 
----> 1 print(input())
 /usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password) 
    743         if value == '\x04': 
   744             # EOF 
-->745             raise EOFError 
    746         return value 
     747   

EOFError: 
(*) You can kind of think of string concatenation as "gluing" strings together.
(**) Note that they must be strings, otherwise you will get a TypeError (see Basic Syntax). You can bypass this by using the str() command, which will try to change the type of the thing inside the parentheses to a string. I'll explain this in more detail later.
(***) On Unix/Linux systems, pressing Ctrl+D will send the EOF signal, whereas on a Windows machine, pressing Ctrl+Z+Enter will do this.
(****) In the error, we have the string '\x04'. So, hex character 04 is the EOF character (recall \xhh inside a string means to escape the character with hex value hh. In ASCII, hex 04 is not defined to anything, and hex 26 is reserved for the DOS end of file character. In general this is not really important since in most cases you will have non empty input.

Last fiddled with by Dylan14 on 2020-08-11 at 14:07 Reason: fix another code block, delete extra return lines
Dylan14 is offline