Open in TheBrain

python STRINGS

<p>Jan 24, 2013 12:52 PM &#160;see below for SPLIT

&#160;

&#62;&#62;&#62; name1 = 'bob'

&#160;

&#62;&#62;&#62; name1

'bob'

&#62;&#62;&#62; name2 = 'blum'

&#160;

&#62;&#62;&#62; <b>fullname = name1 + name2 </b>&#160;&#160;....

&#160;

&#62;&#62;&#62; fullname

'bobblum'

&#160;

&#62;&#62;&#62; bkwd =<b> fullname[7:0:-1] + fullname[0]</b>

&#160;

&#62;&#62;&#62; bkwd

'mulbbob'

&#160;

fullname[0:3] =&#62; 'bob' &#160;&#160;## the first 3 chars in the string starting at the zeroeth.

&#160;

&#160;

string concat method

create a list

sean = ['a','b']

''.join(sean) &#160;=&#62; &#160;&#160;&#160;&#160;&#160;&#160;&#160;'ab'

or if u do

',' &#160;it will =&#62; &#160;'a,b' &#160;&#160;string with 3 chars

&#160;

pets

['dog', 'cat', 'gerbil']

&#160;

&#62;&#62;&#62; petstring = ''.join(pets) &#160;&#160;#note here: that is NOT a double quote; it is two single quotes

#ie, that is the character (null string) to insert into the concatenated strings.

&#62;&#62;&#62; petstring

'dogcatgerbil'

&#62;&#62;&#62; pets

['dog', 'cat', 'gerbil']

&#160;

&#62;&#62;&#62; petstring = '&#38;'.join(pets) &#160;# here an &#38; will be inserted btwn instead of a null string.

&#160;

&#62;&#62;&#62; petstring

'dog&#38;cat&#38;gerbil'

&#160;

&#62;&#62;&#62; newpets = petstring[0:3] + petstring[4:7]

&#62;&#62;&#62; newpets

'dogcat'

&#160;

&#160;SPLIT

&#160;

&#62;&#62;&#62; 'klfjdlfd ieutoiweut 394853'.split(' ') =&#62; &#160;[strg1 ,strg 2 , &#160;strg3] &#160;# this SPLITs the string

&#160;

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;# using space as the break char... ie delimited by whatever is btwn the quotes,

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;# &#160;so, &#160;'$' &#160;wll break at the $ .

&#160;

&#62;&#62;&#62; sample = "sljflksd$sljdlfsj$wioreu"

&#160;

&#62;&#62;&#62; sample

'sljflksd$sljdlfsj$wioreu'

&#160;

&#62;&#62;&#62; samplewords = sample.split('$')

&#160;

&#62;&#62;&#62; samplewords

['sljflksd', 'sljdlfsj', 'wioreu'] &#160;&#160;&#160;SPLIT

&#160;

&#160;</p>