String can be expressed in Python by several ways. They can be enclosed in single quotes or double quotes:
One thing to note is that in a string, a single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added.
For example
>>>"This is the first sentence.\ This is the second sentence."'This is the first sentence. This is the second sentence.'
Note the operator ".\" used to enter the string in the next line without changing its structure
Try some string operations using Python
>>>stri = 'programming' >>>print stri # Prints complete string >>>print stri[0] # Prints first character of the string >>>print stri[2:5] # Prints characters starting from 3rd to 6th >>>print stri[2:] # Prints string starting from 3rd character
Concatenation and repetition in python
The plus (+) sign is the string concatenation operator, and the asterisk (*) is the repetition operator.
>>>str1 = 'Enjoy' >>>str2 = 'Python' >>>print str1+str2 # combine two strings str1 and str2 >>>print str1*10 # Prints string ten times
We can find a particular string by ".find" method
>>> S = 'yyyyyyyyyyyyyHIDDENaaaaaaaaaaaa' >>> where = S.find('HIDDEN')
>>> where
isalpha() returns true if all characters in the string are alphabetic and there is at least one character, false otherwise.
>>>'allstring'.isalpha() >>>'allstring1'.isalpha()
isdigit() returns true if all characters in the string are digits
>>>'123'.isdigit()
isspace() returns true if there are only whitespace characters
>>>'123'.isdigit()
UNICODE in Python
import unicodedata
mal=u'എനിക്ക് മലയാളം അറിയാം'
mal=s.encode('utf-8').decode('utf-8')
print(mal)
Now check the following
>>>''This Is A Title'.istitle() >>> print ''.join(seq)
S = 'Holiday' S = S.replace('Holi','Working')
>>> import re >>> str = 'My life; My decision' >>> str2 = re.split('; |, | ',str) >>> str2
To get more information about methods for an object , Call the built-in dir function. It returns a list of all the attributes available for a given object. To get the information about what they do, we can pass them to the help function: dir(S) help(S.replace)