In previous article strings in python we studied in detail that how escape characters \n,\t etc works. In this article we will see how to “escape” or avoid escape characters by using special functionalities provided by Python. These new techniques makes Python most popular language among all languages. Lets take the same example of \n escape character. You know that \n is used to insert line break.
## Escape character end of line print("Hello world \ni am Python")
The output is:
I am Python
Instead of using \n, Python provides another way to write a string in more flexible way. Write your string within (” ” “) three quotes and you are done. See the following example.
## String in quotes print("""Hello World, I am Python, I am the most famous language among all programming languages because of my flexibility and efficiency.""")
and the output is:
I am Python, I am the most famous
language among all programming
languages because of my
flexibility and efficiency.
The output is exactly the based on “what you type is what you get”
Another interesting situation which we have already discussed is, print a string “Copy files from c:\notes directory”
## print following example and check output print("Copy files from c:\notes directory")
Copy files from c:
backslash “\” escape character helps to solve this problem.
## add backslash print("Copy files from c:\\notes directory")
There is another way to avoid this situation. Add ‘r’ at the beginning of the string. R stands for raw value. Python will ignore all escape characters and display what is written in string.
## before starting string type 'r' print(r"Copy files from c:\\notes directory")
Strings Formatters %d, %s, %f, %r
As we have already discussed in previous article strings in python that Python provides various ways to decorate a string for cleaner and nicer output including combining two string, escape characters, string multiplication and so on. Lets discuss one more powerful feature provided by Python called string formatters. These formatters can be inserted anywhere within the string.
## %d for integer formatter x = "In my class there are %d girls and %d boys" %(10 , 5) print (x)
and the output is “In my class there are 10 girls and 5 boys”. Insert your required formatter within your string and assign some values at the end. %d is the formatter which handles integer type of values. Check another example for float values.
## %f for float formatter pie = ("Value of pie is %f" % 3.14159) print(pie)
Value of pie is 3.141590
For handling strings, %s formatter is ready to do this job, see this example:
## %s for string formatter boy_name = "Bob" girl_name = "Charl" info = "My name is %s and my friend name is %s" %(girl_name,boy_name) print(info)
In previous section you have already seen a formatter ‘r’, infact the %r formatter can be used to display any type of value in string. See the following example:
## take variable boy_name and girl_name from previous example boys_in_class = 5 girls_in_class = 10 info1 = "My name is %r and my friend name is %r" %(girl_name, boy_name) info2 = "In my class there are %r girls and %r boys" %(boys_in_class, girls_in_class) info3 = "and I know that the value of pie is %r" %(pie) print(info1,info2,info3)
and output is : My name is ‘Charl’ and my friend name is ‘Bob’, In my class there are 10 girls and 5 boys and I know that the value of pie is 3.14159. Very convenient.