Thursday, 5 July 2012

String special Operators: in Python


Operator
Description
Example
+
Concatenation – Adds values on either
side of the operator
a + b will give HelloPython
*
Repetition – Creates new strings, concatenating multiple copies of the same string
a*2 will give -HelloPython
[]
Slice-Gives the character from the given index
a[1] will give e
[ : ]
Range Slice – Gives the characters from the given range
a[1:4] will give ell
In
Membership – Returns true if a character exists in the given string
H in a will give 1
Not in
Membership – Returns true if a character does not exist in the given string.
M not in a will give 1
r/R
Raw String – Suppress actual meaning of Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter “r,” which precedes the quotation marks. The “r” can be  lowercase(r) or uppercase (R) preceding the first quote mark.
Print r’\n’ prints \n and prints R’\n’ prints\n
%
Format – Performs String formatting

No comments:

Post a Comment