String is widely used data type in Python. Other variables like integer, float or boolean contains one specific type of value but Strings can contain collection of different values in well structured way. Everything in between double quotes is considered as a string. Python would not mind if it is in single quote.
## String in quotes print(" This is a string ") print( ' This is also a string ' )
Strings are actually a first data structure which provides a huge number of functions which makes programmer’s life easy. If i tell you that you can combine two strings with “+” plus sign, you wouldn’t be surprised because other programming languages offer this feature but what if i tell you that you can multiply string by using “*” multiply sign!!!!. Yes you can do that and you will see later in this article. Lets take some simple example to print the string on screen. Create two variable name and profession assign some value in (“) or (‘) quote. Open IDLE shell and type following example and see the output.
## string variables name = "Abid Khan" profession = 'Software Engineer' print(name) print(profession)
Like other modern programming languages, Python also does not require to declare variable type (String name, String profession) for handling strings. As you can see in this example, once the value in quotes is assigned to the variable, Python would consider it as a string variable.
Let’s see how string is stored in memory. Create a new variable language and print it on the screen.
## String variable language language = "Python" print(language)
When a string variable is stored in memory, every single character is assigned an individual memory address and it looks like this:
Each character in language variable has its own index. P has index 0, y has 1 and n has index 5. Each and every character in this string can be accessed by it’s index. To access the specific character in string use [ ], for example language to access P at index 0. Add the following lines of code in your example and see the output
print(language) # this line will print value P at index 0 print(language) # this line will print value h at index 3 print(language) # this line will print value n at index 5 print(language,language,language,language,language,language)
you should see:
P y t h o n
Python not only assign 0 based index to the sequence of characters it also assign negative index as well and this -ve index starts from last character of the string with -1. The image of negative indexing is as follow:
add following lines of code in your example and you will get the same result:
print(language[-6]) # this line will print value P at index -6 print(language[-3]) # this line will print value h at index -3 print(language[-1]) # this line will print value n at index -1 at last index # print value n at last index by using len function print(language[len(language)-1])
Notice that it is so easy to access last character of the string with -1, if string is too long or you have no idea how to use built-in len function.
Like other programming languages, python provides a handy way to add or combine two strings by using + sign. Add following example with plus sign and see the output.
name = "Abid Khan" ## string variable name profession = 'Software Engineer' ## string variable profession info = name + " " + profession ## combine both variables with " " space and assign to info variable print(info) ## print this info
Try this example and change some values.
Python is also very good in multiplication with strings. Want to print srting 3 times ? multiply with 3. Check this example
language = "Python" print(language * 3)
String and escape characters \n, \t, \\
Python provides a list of escape character which can be used to insert non-alphabetic characters into the string. Escape character starts with backslash “\” and it can be inserted anywhere inside the string: for example \n,which divides the string into two lines. Try following code and see how escape character works.
## Escape character end of line print("Hello world \ni am Python") ##The output is: ##Hello world ##I am Python
If you want to add tab in your string, add \t.
# Escape character Tab print("Hello world \ti am Python") ## The output is ## Hello world I am Python
Print function prints everything within the (” “) quotes on the screen but sometime you want to print double quotes as well. What if your required out is as follow:
Hello world “i am Python” !
Python will throw an error for this code.
## something fishy here print("Hello world "I am Python"")
add \ with each (“) quote.
# Escape character quotes print("Hello world \" i am Python\" !")
Check another interesting situation, if you want to print following string
“Copy my files from c:\notes directory”
add following line in your python file and see the output.
print("Copy my files from c:\notes directory") ## Output is: Copy my files from c: otes directory
You’ve received a bit strange result and reason is \n escape character, python considers this c:\notes contains \n which is end of line in normal circumstances. Adding another “\” can solve the problem. See the following code
print("Copy my files from c:\\notes directory") ## Output is ## Copy my files from c:\notes directory.
See the available escape characters which are provided by python.
form feed (FF)
line feed (LF)
carriage return (CR)
normal tab (TAB)
vertical tab (VT)