Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> S = "Hello World" >>> len(S) 11 >>> S[0] 'H' >>> S[10] 'd' >>> S[11] Traceback (most recent call last): File "", line 1, in S[11] IndexError: string index out of range >>> x = 1+1 >>> x 2 >>> type(x) >>> type(S) >>> T = S + S >>> T 'Hello WorldHello World' >>> len(T) 22 >>> T[11] 'H' >>> T = S + " " + S >>> len(T) 23 >>> T[0] 'H' >>> T[11] ' ' >>> T[21] 'l' >>> T[22] 'd' >>> T[23] Traceback (most recent call last): File "", line 1, in T[23] IndexError: string index out of range >>> ================================ RESTART ================================ >>> Please type some text: Monday First, we print each character in order: S[ 0 ] = M S[ 1 ] = o S[ 2 ] = n S[ 3 ] = d S[ 4 ] = a S[ 5 ] = y Type enter to continue> Traceback (most recent call last): File "C:\Users\ludaesch\Dropbox\10-FQ-2012\str1.py", line 10, in input("\nType enter to continue>\n") KeyboardInterrupt >>> S = "a\nb\nc" >>> len(S) 5 >>> list(range(5)) [0, 1, 2, 3, 4] >>> len(S) 5 >>> for i in range(len(S)): print(i, ">>>" + S[i] + "<<<") 0 >>>a<<< 1 >>> <<< 2 >>>b<<< 3 >>> <<< 4 >>>c<<< >>> print(S) a b c >>> S 'a\nb\nc' >>> ================================ RESTART ================================ >>> Please type some text: Monday First, we print each character in order: S[ 0 ] = M S[ 1 ] = o S[ 2 ] = n S[ 3 ] = d S[ 4 ] = a S[ 5 ] = y Type enter to continue> Same idea, but less explicit: S[ 0 ] = M S[ 1 ] = o S[ 2 ] = n S[ 3 ] = d S[ 4 ] = a S[ 5 ] = y Type enter to continue> Now all in one line: S[0]=M S[1]=o S[2]=n S[3]=d S[4]=a S[5]=y Type enter to continue> >>> ================================ RESTART ================================ >>> Please type some text: Tuesday First, we print each character in order: S[ 0 ] = T S[ 1 ] = u S[ 2 ] = e S[ 3 ] = s S[ 4 ] = d S[ 5 ] = a S[ 6 ] = y Type enter to continue> Same idea, but less explicit: S[ 0 ] = T S[ 1 ] = u S[ 2 ] = e S[ 3 ] = s S[ 4 ] = d S[ 5 ] = a S[ 6 ] = y Type enter to continue> Now all in one line: S[0]=T; S[1]=u; S[2]=e; S[3]=s; S[4]=d; S[5]=a; S[6]=y; Type enter to continue> >>> print(10,17,15, sep=";") 10;17;15 >>> print(10+17+15, sep=";") 42 >>> print(10,17,15, sep=";", end="!") 10;17;15! >>> S 'Tuesday' >>> for c in S: print(c) T u e s d a y >>> for c in S: print(c, end = ";") T;u;e;s;d;a;y; >>> L = list(range(10)) >>> L [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> L[3] 3 >>> L = list(range(10,10)) >>> L [] >>> L = list(range(10,100,10)) >>> L [10, 20, 30, 40, 50, 60, 70, 80, 90] >>> L[0] 10 >>> L[1] 20 >>> L[2] 30 >>> N = len(L) >>> N 9 >>> L[8] 90 >>> L[9] Traceback (most recent call last): File "", line 1, in L[9] IndexError: list index out of range >>> L[3] = L[3] +1 >>> L [10, 20, 30, 41, 50, 60, 70, 80, 90] >>> S = "Hello World" >>> S 'Hello World' >>> S[0] 'H' >>> S[1] 'e' >>> S[3] 'l' >>> S[3] = "k" Traceback (most recent call last): File "", line 1, in S[3] = "k" TypeError: 'str' object does not support item assignment >>> T Traceback (most recent call last): File "", line 1, in T NameError: name 'T' is not defined >>> S 'Hello World' >>> T = S + "!!" >>> T 'Hello World!!' >>> S 'Hello World' >>> for c in S: print(c) H e l l o W o r l d >>> S 'Hello World' >>> for c in S: print(c, end=".") H.e.l.l.o. .W.o.r.l.d. >>> for i in range(len(S)): print(i, S[i]) 0 H 1 e 2 l 3 l 4 o 5 6 W 7 o 8 r 9 l 10 d >>> for i in range(len(S)): print(i, S[11-i]) Traceback (most recent call last): File "", line 2, in print(i, S[11-i]) IndexError: string index out of range >>> for i in range(len(S)): print(i, S[10-i]) 0 d 1 l 2 r 3 o 4 W 5 6 o 7 l 8 l 9 e 10 H >>> for i in range(len(S)): print(i, S[10-i], end =" ") 0 d 1 l 2 r 3 o 4 W 5 6 o 7 l 8 l 9 e 10 H >>> for i in range(len(S)): print(S[10-i], end ="") dlroW olleH >>> S = "a" >>> len(S) 1 >>> S = "" >>> len(S) 0 >>> for i in range(len(S),0, -1): print(i, S[i]) >>> S '' >>> len(S) 0 >>> S = "Monday" >>> for i in range(len(S),0, -1): print(i, S[i]) Traceback (most recent call last): File "", line 2, in print(i, S[i]) IndexError: string index out of range >>> list(range(len(S),0,-1)) [6, 5, 4, 3, 2, 1] >>> for i in range(len(S),0, -1): print(i, S[i-1]) 6 y 5 a 4 d 3 n 2 o 1 M >>> for i in range(len(S),0, -1): idx = i-1 print(idx, S[idx]) 5 y 4 a 3 d 2 n 1 o 0 M >>> S 'Monday' >>> S = S + "foobar" >>> S 'Mondayfoobar' >>> S[1] = "c" Traceback (most recent call last): File "", line 1, in S[1] = "c" TypeError: 'str' object does not support item assignment >>> S 'Mondayfoobar' >>> T = "" >>> for c in S: print(T,c) T = T + c M M o Mo n Mon d Mond a Monda y Monday f Mondayf o Mondayfo o Mondayfoo b Mondayfoob a Mondayfooba r >>> for c in S: print(T,c,sep=":") T = T + c Mondayfoobar:M MondayfoobarM:o MondayfoobarMo:n MondayfoobarMon:d MondayfoobarMond:a MondayfoobarMonda:y MondayfoobarMonday:f MondayfoobarMondayf:o MondayfoobarMondayfo:o MondayfoobarMondayfoo:b MondayfoobarMondayfoob:a MondayfoobarMondayfooba:r >>> T = "" >>> for c in S: print(T,c,sep=":") T = T + c :M M:o Mo:n Mon:d Mond:a Monda:y Monday:f Mondayf:o Mondayfo:o Mondayfoo:b Mondayfoob:a Mondayfooba:r >>> T 'Mondayfoobar' >>> len(T) 12 >>> len(S) 12 >>> T 'Mondayfoobar' >>> S 'Mondayfoobar' >>> T = "" >>> for c in S: print(T,c,sep=":") T = c + T :M M:o oM:n noM:d dnoM:a adnoM:y yadnoM:f fyadnoM:o ofyadnoM:o oofyadnoM:b boofyadnoM:a aboofyadnoM:r >>> T 'raboofyadnoM' >>> Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> food = "pizza" >>> food[0:3] 'piz' >>> food[3] 'z' >>> food[4] 'a' >>> food[0:1] 'p' >>> food[-1] 'a' >>> food[-2] 'z' >>> food[-3] 'z' >>> food[1:3] 'iz' >>> food[1:3:-1] '' >>> food[-1:-3] '' >>> food[-1] 'a' >>> food[-1:0] '' >>> food[-1:0]