>>> a = "string in a double quote" >>> b = 'string in a single quote' >>> print(a) string in a double quote >>> print(b) string in a single quote >>> # using ',' to concate the two or several strings >>> print(a,"concated with",b) string in a double quote concated with string in a single quote >>> #using '+' to concate the two or several strings >>> print(a+" concated with "+b) string in a double quote concated with string in a single quote >>>
Python does not support a character type; these are treated as strings of length one, thus also considered a substring.
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.
>>> var1 = 'Hello World!' >>> var2 = "Python Programming Language" >>> print (var1) H >>> print (var2) P >>> print(var2[2:7]) thon >>>
There are various string operators that can be used in different ways like concatenating different string.
|+||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 -HelloHello|
|||Slice - Gives the character from the given index||a will give e|
|[ : ]||Range Slice - Gives the characters from the given range||a[1:4] will give ell|
|%||Format - Performs String formatting||See at next section|
|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 suppresses actual meaning of escape characters.||Print r'\n' prints \n and print R'/n' prints \n|
One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C's printf() family.
print "My name is %s and weight is %d kg!" % ('Ramana Reddy', 63)
When the above code is executed.
My name is Ramana Reddy and weight is 63 kg!
Here is the list of complete set of symbols which can be used along with %
|%s||string conversion via str() prior to formatting|
|%i||signed decimal integer|
|%X||hexadecimal integer (UPPERcase letters)|
|%x||hexadecimal integer (lowercase letters)|
|%d||signed decimal integer|
|%u||unsigned decimal integer|
|%e||exponential notation (with lowercase 'e')|
|%E||exponential notation (with UPPERcase 'E')|
|%f||floating point real number|
|%g||the shorter of %f and %e|
|%G||the shorter of %f and %E|
Other supported symbols and functionality are listed in the following table −
|*||argument specifies width or precision|
|+||display the sign|
|<sp>||leave a blank space before a positive number|
|%||'%%' leaves you with a single literal '%'|
|(var)||mapping variable (dictionary arguments)|
|#||add the octal leading zero ( '0' ) or hexadecimal leading '0x' or '0X', depending on whether 'x' or 'X' were used.|
|0||pad from left with zeros (instead of spaces)|
|m.n.||m is the minimum total width and n is the number of digits to display after the decimal point (if appl.)|
The method replace() returns a copy of the string in which the values of old string have been replaced with the new value.
oldstring = 'Welcome to Free Time Learning' newstring = oldstring.replace('Free Time Learning', 'F T L') print(newstring)
In Python, you can even change the string to upper case or lower case.
string="python at free time learn" print string.upper()