Like Java programming language, str objects in python is also an immutable datatype.
name="james"
OR
name='james'
Please note that use of single quote(') or double quote(") should be consistent. If we start a string with single quote(') then it should be ended with that only. Otherwise compiler error will be thrown. Also say if we use single quote and we want use double quote as str character then that is allowed in python.
Example:
len('abc') should produce output 4.
split([sep [,maxsplit]])
How to create str objects?
We can create a string object using below syntax;name="james"
OR
name='james'
Please note that use of single quote(') or double quote(") should be consistent. If we start a string with single quote(') then it should be ended with that only. Otherwise compiler error will be thrown. Also say if we use single quote and we want use double quote as str character then that is allowed in python.
Basic Operations on string:
len(string): This method will return the length of the string.Example:
len('abc') should produce output 4.
split([sep [,maxsplit]])
This method is used to split a string into several strings based on the parameter separator(sep) and max no of split to be done (maxsplit). By default sep is any white space.
Example:
>>> str="I am a graduate."
>>> str.split()
['I', 'am', 'a', 'graduate.']
>>> str.split(" ",2)
['I', 'am', 'a graduate.']
>>> str.split(" ",1)
['I', 'am a graduate.']
Example:
>>> str="I am a graduate."
>>> str.split()
['I', 'am', 'a', 'graduate.']
>>> str.split(" ",2)
['I', 'am', 'a graduate.']
>>> str.split(" ",1)
['I', 'am a graduate.']
No comments:
Post a Comment