Data structures in python continued.. (strings) .. [P-2]

Home Forums Python Programming Data structures in python continued.. (strings) .. [P-2]

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1936 Reply
    Humble
    Keymaster

    Python has different data structures like :


      Strings
      lists
      tuples
      Dictionaries

    Lets start with strings:

    1) Strings can be represented either in double quotes or in single quotes..

    >>>> ex = "I am a string"
    >>> type(ex)
    <type 'str'>
    >>> ex = 'I am a string'
    >>> type(ex)
    <type 'str'>
    >>> ex
    'I am a string'
    >>> 
    >>> 
    
    
    
    2) Strings can be indexed:
    
    
    >
    >>> for i in ex:
    ...     print i
    ... 
    I
     
    a
    m
     
    a
     
    s
    t
    r
    i
    n
    g
    >>> 
    
    >>> ex[0]
    'I'
    >>> ex[1]
    ' '
    >>> ex[2]
    'a'
    >>> ex[3]
    'm'
    >>> 
    
    
    
    
    3) Strings can be concatenated
    
    
    >>>> "Hi "+ex
    'Hi I am a string'
    >>>
    
    
    4)  some important methods/functions  of "string"  :
    
    
      upper() lower() split() strip() join() replace() ....
    Examples on upper() and lower()
    >>>> ex
    'I am a String'
    >>> 
    
    >>> ex.upper()
    'I AM A STRING'
    >>> ex.lower()
    'i am a string'
    
    >>>
    
    
    
    |  split(...)
    |      S.split([sep [,maxsplit]]) -> list of strings
    |
    |      Return a list of the words in the string S, using sep as the
    |      delimiter string.  If maxsplit is given, at most maxsplit
    |      splits are done. If sep is not specified or is None, any
    |      whitespace string is a separator and empty strings are removed
    |      from the result.
    |
    
    
    You can specify the delimiter , if so, it will be splitted based on that 'delimiter', if not, it will be splitted based on "whitspace"..
    
    see, below example:
    
    
    >>>> ex= 'I am good: how are you'
    >>> ex
    'I am good: how are you'
    >>> ex.split(':')
    ['I am good', ' how are you']
    >>> ex
    'I am good: how are you'
    >>> ex.split()
    ['I', 'am', 'good:', 'how', 'are', 'you']
    >>> 
    
    
    
    
     |
    |  strip(...)
    |      S.strip([chars]) -> string or unicode
    |
    |      Return a copy of the string S with leading and trailing
    |      whitespace removed.
    |      If chars is given and not None, remove characters in chars instead.
    |      If chars is unicode, S will be converted to unicode before stripping
    
    
    
    >
    >>> ex = '     I am good: and how are you  '
    >>> ex
    '     I am good: and how are you  '
    >>> ex.strip()
    'I am good: and how are you'
    >>> ex
    '     I am good: and how are you  '
    
    >>> ex= 'malayalam'
    >>> ex.strip('m')
    'alayala'
    >>> ex.strip('a')
    'malayalam'
    >>> 
    
    
    
    When using strips please think that its going to strip only from start and end, and it returns a copy.. The original string is intact..
    
    
    
     |  replace(...)
    |      S.replace(old, new[, count]) -> string
    |
    |      Return a copy of string S with all occurrences of substring
    |      old replaced by new.  If the optional argument count is
    |      given, only the first count occurrences are replaced.
    
    
    
    >>>> ex
    'malayalam'
    >>> ex.replace('a','j',3)
    'mjljyjlam'
    >>> ex.replace('al','jk')
    'mjkayjkam'
    >>> ex.replace('al','')
    'mayam'
    >>> 
    
    
    
     |  join(...)
    |      S.join(iterable) -> string
    |
    |      Return a string which is the concatenation of the strings in the
    |      iterable.  The separator between elements is S.
    
    
    
    
    >>>> ex
    'malayalam'
    >>> ':'.join(ex)
    'm:a:l:a:y:a:l:a:m'
    
    >>> "Hi".join(ex)
    'mHiaHilHiaHiyHiaHilHiaHim'
    >>> 
    
    
    
    
    It continues... how-ever some more examples
    
    index example
    
    
    
    >>>> ex
    'malayalam'
    >>> ex.index('y')
    4
    >>> ex[4]
    'y'
    >>>
    
    
    
    count example:
    
    
    >>>> ex.count('a')
    4
    >>> ex.count('m')
    2
    >>> ex.count('l')
    2
    >>> 
    
    
    length of the string
    
    
    >>>> ex
    'malayalam'
    >>> len(ex)
    9
    >>> ex="Hi hope it helps"
    >>> len(ex)
    16
    >>> 
    
    
    
    
    Strings can be used with format specifiers..
    
    
    >>>> print ("hi " + "%s" + " dude") %(ex)
    hi hope it helps dude
    
    
    
    
    
    How-ever strings are immutable:
    
    
    >>>> ex[1]
    'o'
    >>> ex[2]
    'p'
    >>> ex
    'hope it helps'
    >>> ex[2]='k'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object does not support item assignment
    >>>
    
    
    
    
    you can convert any other data types to strings by 'str'
    
    
    
    >>>> x
    1
    >>> type(x)
    <type 'int'>
    >>> str(x)
    '1'
    >>> type(str(x))
    <type 'str'>
    
    
    
    
    Shoot your queries about python strings here.....
    #2107 Reply
    Humble
    Keymaster

    Some more slicing examples:

    >>> str= "humble"
    >>> str[::-2]
    'ebu'
    >>> str[::2]
    'hml'
    >>> str[1:2]
    'u'
    >>> str[1::]
    'umble'
    >>> str[::3]
    'hb'
    >>> str[:3]
    'hum'
    >>> 
    
    
Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Data structures in python continued.. (strings) .. [P-2]
Your information: