Python Strings
A string is a sequence of characters and a character is can be a alphabet, digit or symbol.
The Python treated any sequence of character’s enclosed in quotes as a string.
1 2 | "Welcome" 'Welcome' |
Python treated both single and double quted sequence as string. But in results of any string will be in a single quote only. like:
‘Welcome’
String Concatenation
In Python strings have operation symbols too. Using a plus (+) symbol concatenate two strings. let’s type below example on Python shell.
1 2 | 'Hello ' + 'World' 'Hello ' + "World" |
The result will be same
‘Hello World’
Next, try with some digits enclosed with and without quotes.
1 2 | '2' + '5' 2 + 5 |
Python checks for the types of values, based on that interprets the plus symbol:
Results:
’25’
7
What happen when you try to concatenate string with digits in Python.
1 | '2' + 5 |
You will see a type error message like TypeError: can only concatenate str (not “int”) to str
First you need to change type of int to str, then you can concatenate them.
1 | '2' + str(5) |
Result: 7
Triple Quoted String
In Python we can use sngle quote (‘) or double quote (“) to define a type to string. Python also uses triple quote (”’) for defining the multi line string.
Run the below test on Python shell.
1 2 3 4 5 | comment = '''Welcome to the "Python" tutorial on tecadmin.net''' print(comment) |