Lets try to learn some useful function in the str class
In windows new lines should be represented by the carriage return at new line couplet \r\n. In linux it is simply \n
Operating System | New line character |
---|---|
Windows | \r\n |
Linux | \n |
In python new line character is \n
We can use python built in function len() to find Length of a String
Join is a method on str which takes a collection of strings as an argument and produces a new string by inserting a separator between each of them.
Join using empty
Strings can be splitted using split function
It divides a string into three sections, the part before the separator, the separator itself, and the part after the separator. The partition() method divides a string into three around a separator: prefix, separator, suffix
>>> source,seperator,destination="LondonToNewyork".partition("To")
>>> source
'London'
>>> seperator
'To'
>>> destination
'Newyork'
Using underscore
>>> source,_,destination="LondonToNewyork".partition("To")
>>> source
'London'
>>> destination
'Newyork'
>>> _
'To'
Note: _ underscore variable is for unused or dummy values
Token Replacement in python can be done using format function
You can repeat the index
>>> "The age of {0} is {1} years.{0} is very old".format("sun",4.6e9)
'The age of sun is 4600000000.0 years.sun is very old'
Keyword arguments
>>> "The age of {name} is {age} years".format(name="sun",age=4.6e9)
'The age of sun is 4600000000.0 years'
You can pass tuple or list
>>> "The age of {position[0]} is {position[1]} years".format(position=["sun",4.6e9])
'The age of sun is 4600000000.0 years'
Pass object
>>> import math
>>> "Math constants pi={m.pi}, Euler's number e={m.e}".format(m=math)
"Math constants pi=3.141592653589793, Euler's number e=2.718281828459045"