Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

Saturday, November 2, 2013

Play with Strings !

STRINGS

LOVE IT !! Besides numbers, Python can also manipulate strings, which can be expressed in several ways.


#! python works with strings
# Used 1 to 24 in printing to see the results in console.

# Strings can be concatenated (glued together) with the + operator, and repeated with *: 

word = 'Help' + 'A'
print 1,word

print 2,'<' + word*5 + '>'

# Two string literals next to each other are automatically concatenated; 
# the first line above could also have been written "word = 'Help' 'A'"; 
# this only works with two literals, not with arbitrary string expressions: 

st='str' 'ing'             #  <-  This is ok
print 3, st
st='str'.strip() + 'ing'   #  <-  This is ok
print 4, st

# Strings can be subscripted (indexed); like in C, the first character of a string 
# has subscript (index) 0. There is no separate character type; a character is 
# simply a string of size one. Like in Icon, substrings can be specified with 
# the slice notation: two indices separated by a colon. 

print 5, word[4]

print 6, word[0:2]

print 7, word[2:4]

# Slice indices have useful defaults; an omitted first index defaults to zero, 
# an omitted second index defaults to the size of the string being sliced. 

print 8, word[:2]    # The first two characters
print 9, word[2:]    # All but the first two characters

# Python strings cannot be changed. Assigning to an indexed position in the string results in an error: 
# However, creating a new string with the combined content is easy and efficient: 

print 10, 'x' + word[1:]

print 11, 'Splat' + word[4]

# Here's a useful invariant of slice operations: s[:i] + s[i:] equals s. 

print 12, word[:2] + word[2:]

print 13, word[:3] + word[3:]


# Degenerate slice indices are handled gracefully: an index that is too large is replaced 
# by the string size, an upper bound smaller than the lower bound returns an empty string. 

print 14, word[1:100]

print 15, word[10:]

print 16, word[2:1]


# Indices may be negative numbers, to start counting from the right. For example: 


print 17, word[-1]     # The last character

print 18, word[-2]     # The last-but-one character

print 19, word[-2:]    # The last two characters

print 20, word[:-2]    # All but the last two characters


# But note that -0 is really the same as 0, so it does not count from the right! 

print 21, word[-0]     # (since -0 equals 0)

# Out-of-range negative slice indices are truncated, but don't try this for single-element (non-slice) indices: 

print 22, word[-100:]

# print word[-10]    # error

#The best way to remember how slices work is to think of the indices as pointing between characters, 
#with the left edge of the first character numbered 0. Then the right edge of the last character 
#of a string of n characters has index n, for example: 

# +---+---+---+---+---+ 
# | H | e | l | p | A |
# +---+---+---+---+---+ 
# 0   1   2   3   4   5 
#-5  -4  -3  -2  -1


s = 'supercalifragilisticexpialidocious'
print 23, s
print 24, 'length of above string is : ', len(s)

http://www.codeskulptor.org/#user22_0UcuKWvNvVDJS7H.py

Output :


1 HelpA
2 <HelpAHelpAHelpAHelpAHelpA>
3 string
4 string
5 A
6 He
7 lp
8 He
9 lpA
10 xelpA
11 SplatA
12 HelpA
13 HelpA
14 elpA
15 
16 
17 A
18 p
19 pA
20 Hel
21 H
22 HelpA
23 supercalifragilisticexpialidocious
24 length of above string is :  34