How to Add Two Strings to Python
Build scripts using the Python programming language to parse and format text strings that are posted on websites or documents. If a Python program receives user input that must be joined together to form a single line, the Python concatenation feature can be employed to create one string out of two. Use the "+" or "concatenation" operator when you need to combine two strings of characters into a single line using Python.
Instructions
-
-
1
Open a command-line terminal console on a computer that has the Python programming language installed. Type "python" at the command line and tap the "Enter" key.
-
2
Type "number = seven" without quotes and press the "Enter" key. Enter "print (number)" on the command line and tap the "Enter" key. Note that the string "seven" is displayed on the command line. Python uses the keyword "print" to display information to standard output (which is the computer screen).
-
-
3
Enter "string1 = "my string"" without the outside quotes at the command prompt except replace "my string" with a string of ASCII characters you wish to add to another string of ASCII characters. Press the "Enter" key.
-
4
Write "string2 = "second string"" without the outside quotes on the command line except replace "second string" with ASCII characters you want to add to the first string of ASCII characters just entered. Press the "Enter" key.
-
5
Type "print ("string1" + "string2")" at the command prompt and tap the "Enter" key. Note that using the "+" operator added the two string variables together without placing a space between them and then displayed the two strings as one string to standard output.
-
6
Enter "print ("string1 " + "string2")" on the command line and press the "Enter" key. Note that by placing a single space immediately after the "string1" variable the two strings are added together and properly formatted so that there is now a space between the first and second strings posted to standard output.
-
1
References
- Photo Credit Stockbyte/Stockbyte/Getty Images