Python strings Function
String assignment using '
or "
:
>>> s = 'EndMemo.com' >>> s2 = "EndMemo.com" >>> s == s2
True
String is can be treated as a list of characters:
>>> s[0]
'E'
>>> s[3:6]
'Mem'
Search for substring:
>>> x = 'EndMemo Python Tutorial' >>> 'P'in x
True
>>> 'x'not in x
True
String concatenation use
"+"
, "+="
operators or join()
Method.
>>> str = "python" >>> str += " tutorial" >>> str
"python tutorial"
>>> str2 = "EndMemo" >>> str3 = " -- ".join((str,str2)) >>> str3
"python tutorial -- EndMemo"
>>> str = "python" >>> str2 = " tutorial" >>> str3 = str + " " + str2 >>> str3
'python tutorial'
split()
method divide a string based on specified separator.>>> x = 'EndMemo Python Tutorial' >>> x.split('th')
['EndMemo Py', 'on Tutorial']
>>> x.split('o')
['EndMem', ' Pyth', 'n Tut', 'rial']
>>> x.split('o',1)
['EndMem', ' Python Tutorial']
String formatting for print:
>>> print ("%s is No. %d" % ("endmemo.com", 1))
endmemo.com is No. 1
Python string format symbol list:
Symbol | Description |
%c | character |
%s | string |
%d | signed decimal integer |
%i | integer |
%u | unsigned decimal integer |
%o | octal integer |
%f | floating number |
%e | exponential notation with lowercase e |
%E | exponential notation with uppercase E |
%x | hexadecimal integer in lowercase |
%X | hexadecimal integer in uppercase |