📄 node16.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0062)http://www.honors.montana.edu/~jjc/easytut/easytut/node16.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Revenge of the Strings</TITLE>
<META content="Revenge of the Strings" name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK
href="node16_files/easytut.css" rel=STYLESHEET><LINK href="node17.html"
rel=next><LINK href="node15.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node17.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html"
name=tex2html355><IMG align=bottom alt=next border=0 height=24
src="node16_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html351><IMG align=bottom alt=up border=0 height=24
src="node16_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html345><IMG align=bottom alt=previous border=0 height=24
src="node16_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html353><IMG align=bottom alt=contents border=0 height=24
src="node16_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html"
name=tex2html356>File IO</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html352>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html346>More on Lists</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html354>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A
name=CHILD_LINKS><STRONG>Subsections</STRONG></A>
<UL>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node16.html#SECTION001610000000000000000"
name=tex2html357>Examples</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION001600000000000000000>Revenge of the Strings</A> </H1>
<P>And now presenting a cool trick that can be done with strings: <PRE>def shout(string):
for character in string:
print "Gimme a "+character
print "'"+character+"'"
shout("Lose")
def middle(string):
print "The middle character is:",string[len(string)/2]
middle("abcdefg")
middle("The Python Programming Language")
middle("Atlanta")
</PRE>
<P>And the output is: <PRE>Gimme a L
'L'
Gimme a o
'o'
Gimme a s
's'
Gimme a e
'e'
The middle character is: d
The middle character is: r
The middle character is: a
</PRE>What these programs demonstrate is that strings are similar to lists in
several ways. The <TT>shout</TT> procedure shows that <TT>for</TT> loops can be
used with strings just as they can be used with lists. The middle procedure
shows that that strings can also use the <TT>len</TT> function and array indexes
and slices. Most list features work on strings as well.
<P>The next feature demonstrates some string specific features: <PRE>def to_upper(string):
## Converts a string to upper case
upper_case = ""
for character in string:
if 'a' <= character <= 'z':
location = ord(character) - ord('a')
new_ascii = location + ord('A')
character = chr(new_ascii)
upper_case = upper_case + character
return upper_case
print to_upper("This is Text")
</PRE>with the output being: <PRE>THIS IS TEXT
</PRE>This works because the computer represents the characters of a string as
numbers from 0 to 255. Python has a function called <TT>ord</TT> (short for
ordinal) that returns a character as a number. There is also a corresponding
function called <TT>chr</TT> that converts a number into a character. With this
in mind the program should start to be clear. The first detail is the line:
<CODE>if 'a' <= character <= 'z':</CODE> which checks to see if a letter
is lower case. If it is than the next lines are used. First it is converted into
a location so that a=0,b=1,c=2 and so on with the line: <CODE>location =
ord(character) - ord('a')</CODE>. Next the new value is found with
<CODE>new_ascii = location + ord('A')</CODE>. This value is converted back to a
character that is now upper case.
<P>Now for some interactive typing exercise: <PRE>>>> #Integer to String
...
>>> 2
2
>>> repr(2)
'2'
>>> -123
-123
>>> repr(-123)
'-123'
>>> #String to Integer
...
>>> "23"
'23'
>>> int("23")
23
>>> "23"*2
'2323'
>>> int("23")*2
46
>>> #Float to String
...
>>> 1.23
1.23
>>> repr(1.23)
'1.23'
>>> #Float to Integer
...
>>> 1.23
1.23
>>> int(1.23)
1
>>> int(-1.23)
-1
>>> #String to Float
...
>>> float("1.23")
1.23
>>> "1.23"
'1.23'
>>> float("123")
123.0
</PRE>
<P>If you haven't guessed already the function <CODE>repr</CODE> can convert a
integer to a string and the function <CODE>int</CODE> can convert a string to an
integer. The function <TT>float</TT> can convert a string to a float. The
<CODE>repr</CODE> function returns a printable representation of something. Here
are some examples of this: <PRE>>>> repr(1)
'1'
>>> repr(234.14)
'234.14'
>>> repr([4,42,10])
'[4, 42, 10]'
</PRE>The <CODE>int</CODE> function tries to convert a string (or a float) into
a integer. There is also a similar function called <CODE>float</CODE> that will
convert a integer or a string into a float. Another function that Python has is
the <CODE>eval</CODE> function. The <CODE>eval</CODE> function takes a string
and returns data of the type that python thinks it found. For example: <PRE>>>> v=eval('123')
>>> print v,type(v)
123 <type 'int'>
>>> v=eval('645.123')
>>> print v,type(v)
645.123 <type 'float'>
>>> v=eval('[1,2,3]')
>>> print v,type(v)
[1, 2, 3] <type 'list'>
</PRE>If you use the <CODE>eval</CODE> function you should check that it returns
the type that you expect.
<P>One useful string function is the <CODE>split</CODE> function. Here's the
example: <PRE>>>> import string
>>> string.split("This is a bunch of words")
['This', 'is', 'a', 'bunch', 'of', 'words']
>>> string.split("First batch, second batch, third, fourth",",")
['First batch', ' second batch', ' third', ' fourth']
</PRE>Notice how <CODE>split</CODE> converts a string into a list of strings.
The string is split by spaces by default or by the optional second argument (in
this case a comma).
<P>
<H1><A name=SECTION001610000000000000000>Examples</A> </H1><PRE>#This program requires a excellent understanding of decimal numbers
def to_string(in_int):
"Converts an integer to a string"
out_str = ""
prefix = ""
if in_int < 0:
prefix = "-"
in_int = -in_int
while in_int / 10 != 0:
out_str = chr(ord('0')+in_int % 10) + out_str
in_int = in_int / 10
out_str = chr(ord('0')+in_int % 10) + out_str
return prefix + out_str
def to_int(in_str):
"Converts a string to an integer"
out_num = 0
if in_str[0] == "-":
multiplier = -1
in_str = in_str[1:]
else:
multiplier = 1
for x in range(0,len(in_str)):
out_num = out_num * 10 + ord(in_str[x]) - ord('0')
return out_num * multiplier
print to_string(2)
print to_string(23445)
print to_string(-23445)
print to_int("14234")
print to_int("12345")
print to_int("-3512")
</PRE>
<P>The output is: <PRE>2
23445
-23445
14234
12345
-3512
</PRE>
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html"
name=tex2html355><IMG align=bottom alt=next border=0 height=24
src="node16_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html351><IMG align=bottom alt=up border=0 height=24
src="node16_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html345><IMG align=bottom alt=previous border=0 height=24
src="node16_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html353><IMG align=bottom alt=contents border=0 height=24
src="node16_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html"
name=tex2html356>File IO</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html352>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html346>More on Lists</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html354>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A>
</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -