Python学习之字符串(二)

    xiaoxiao2021-04-14  70

    转义字符 在字符串中,有时候需要输入特殊的字符,但某些特殊的字符不能直接输出,就需要用到转义了,所谓转义,就是不采用符号本来的含义,而采用另外一种含义。在Python中,常用的转义字符有: \ 在行尾用作换行 \ 反斜杠字符 \’ 单引号 \” 双引号 \a 响铃 \b 退格(backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符号 \t 横向制表符 \r 回车 \f 换页 \oyy 八进制,yy标识字符,\o12表示换行 \xyy 十六进制,yy标示字符,\x0a表示换行 \other 其他字符以普通格式输出 示例演练: 换行:

    >>> print "I wanna \ #这里表示换行 login baidu http://www.baidu.com" I wanna login baidu http://www.baidu.com >>>

    只保留特殊字符:

    >>> print 'D:\\Software\\SoftwareFile\'s\\SoftwareInstall' #这里只保留特殊字符 “\”和 “'” D:\Software\SoftwareFile's\SoftwareInstall >>>

    #上述示例如果不用转义看看是什么意思

    >>> print 'D:\\Software\\SoftwareFile's\\SoftwareInstall' SyntaxError: invalid syntax >>>

    发现报语法错误。

    原始字符串 转义字符就是为了让字符串中的某些字符表示原有的符号,而不被解析成某种具有特别意义的符号。通常,我们把含有特殊字符的字符串都是原始含义的字符串叫做原始字符串;比如反斜杠,就表示的是反斜杠。 示例:

    >>> PATH="D:\\Software\\SoftwareFile\'s\\SoftwareInstall\nishishui" >>> PATH "D:\\Software\\SoftwareFile's\\SoftwareInstall\nishishui" >>>

    这里我们发现貌似没有什么问题。 再来看看这样:

    >>> PATH="D:\\Software\\SoftwareFile\'s\\SoftwareInstall\nishishui" >>> print PATH D:\Software\SoftwareFile's\SoftwareInstall ishishui >>>

    我们发现,出问题了,被换行了。 当然这个问题,我们可以用转义字符来实现,但是如果一直这样,没有意义了。我们用一个字母r来解决以上第一个示例的问题。

    >>> PATH=r"D:\\Software\\SoftwareFile\'s\\SoftwareInstall\nishishui" >>> print PATH D:\\Software\\SoftwareFile\'s\\SoftwareInstall\nishishui >>>

    r开头引起的字符串都被声明为原始字符串,所以,在里面放入任何字符都表示该字符的原始含义 raw_input()和print 这两个都是Python中的内建函数(built-in function) 1、怎么使用raw_input( )函数,这里我们使用Python的help( )来了解一下。

    >>> help(raw_input) Help on built-in function raw_input in module __builtin__: raw_input(...) raw_input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading. >>>

    简单来说就是从标准输入设备读入一个字符串,这个功能其实是一个人机交互过程。下面看示例:

    >>> input = raw_input() this is string >>> print input this is string >>>

    含参的raw_input()

    >>> input = raw_input("Please input your name: ") Please input your name: Lenovo >>> print input Lenovo >>> >>> age = raw_input("How old are you ? ") How old are you ? 20 >>> print age 20 >>> type(age) <type 'str'> >>>

    在这里写一个交互式小程序:

    #!/usr/bin/env python #coding=utf-8 name = raw_input("Please input your name: ") age = raw_input("How old are you ? ") print "Your name is : ",name print "You are "+age+" years old" change_age = int(age) + 3 print "Your\'s age has been changed : "+str(change_age)+" years old" [root@oracle ~]# python dialog.py Please input your name: Lenovo How old are you ? 15 Your name is : Lenovo You are 15 years old Your's age has been changed : 18 years old [root@oracle ~]#

    索引和切片 一下示例不做详解,如果了解java和类C编程语言的同学,相信一看就能够明白。

    lang = “Study python” lang[0] ‘S’ lang[1] ‘t’ lang[:a]

    lang[:1] ‘S’ lang[:3] ‘Stu’ lang[1:3] ‘tu’ lang[1:] ‘tudy python’ lang[:-1] ‘Study pytho’ lang[:-2] ‘Study pyth’ lang[:-4] ‘Study py’

    一些基本的操作: len( )函数:用来统计序列的长度

    >>> string = "I love Python" >>> len(string) 13 >>>

    “+”用来连接两个序列

    >>> a = "this is a string" >>> b = "Oh ,yes !" >>> print a + b this is a stringOh ,yes ! >>>

    In用来判断元素是否在序列中,如果在返回true,如果不在返回false

    >>> condition = "This is a string" >>> 'T' in condition True >>> 'f' in condition False >>>

    max( ),用来返回序列中最大的一个

    >>> a = "This is a string" >>> max(a) 't' >>> b = "8542452" >>> max(b) '8' >>>

    min( ),用来返回序列中最小的一个 如果字符串中含有空格,返回最小的则为空格,如果一个字符串中含有大小写字母,优先返回大写字母中最小的一个。

    >>> a = "This is a string" >>> min(a) ' ' >>> c = "Python" >>> min(c) 'P' >>> c = "python" >>> min(c) 'h' >>> b = "8425459" >>> min(b) '2' >>>

    cmp( ),比较两个字符的大小,先转换为数字,再进行比较

    >>> cmp1 = "a" >>> cmp2 = "b" >>> cmp(cmp1,cmp2) -1 >>> ord(cmp1) 97 >>> ord(cmp2) 98 >>> cmp3="This" >>> cmp4="That" >>> cmp(cmp3,cmp4) 1

    注意,当中的ord只能是字符转换数字,不能是字符串,如果输入字符串,则会提示期待一个字符。 在字符串的比较中,首先从第一个字符进行比较,如果第一个字符相等,则比较下一个,直到遇到不相等的字符,返回1,如果不相等,则返回结果,为-1。直到最后一个还相等,则返回0,位数不够时,按照没有处理,没有不表示0,0表示NUL,位数多的那个大。对于数字的比较,直接比较数字,大则返回1。小则返回-1,相等返回0 示例:

    >>> a = "This" >>> b = "That" >>> cmp(a,b) 1 >>> c = "There" >>> d = "Your" >>> cmp(c,d) -1 >>> e = "1231" >>> f = "4" >>> cmp(e,f) -1 >>> e = 1231 >>> f = 4 >>> cmp(e,f) 1 >>> cmp(f,e) -1 >>> e = 1 >>> f = 1 >>> cmp(e,f) 0 >>>

    “*”符号在Python中表示重复的那个字符串,是很有用的一个符号

    >>> print '#'*30 ############################## >>>

    注:将#号打印30次

    常用的字符串方法 str.isalpha判断是否全为字母,如果是返回true,如果不是返回false

    >>> a.isalpha() True >>> b = "2infinity" >>> b.isalpha() False >>>

    split( ),将字符串按照某个分隔符进行分割

    >>> string = "Python is a script language" >>> string.split(" ") #以空格进行分割 ['Python', 'is', 'a', 'script', 'language'] >>> >>> http = "http:,//,www,.,baidu,.,com" >>> http.split(",") #以逗号进行分割 ['http:', '//', 'www', '.', 'baidu', '.', 'com'] >>>

    对字符串的空格处理 str.strip( ),去掉左右空格 str.lstrip( ),去掉左边空格 str.rstrip( ),去掉右边空格

    >>> a = " this is string " >>> a.strip() 'this is string' >>> a = " This is string " >>> a.lstrip() 'This is string ' >>> a.rstrip() ' This is string' >>>

    字符大小写的转换 str.upper( ) 进行大写转换 str.lower( )进行小写转换 str.capitalize( )第一个字母大写 str.isupper( )判断是否是大写;如果是,返回True,如果不是返回False,以下同 str.islower( ) 判断是否是小写 str.title( )每个字符串的第一个字母大写 str.istitle( ) 判断每个字符串的第一个字母是否为大写

    >>> str = "this is a string" >>> str.upper() 'THIS IS A STRING' >>> str.lower() 'this is a string' >>> str = "This is a string" >>> str.upper() 'THIS IS A STRING' >>> str = "THIS IS A STRING" >>> str.lower() 'this is a string' >>> str = "this is a string" >>> str.capitalize() 'This is a string' >>> str.isupper() False >>> str= "THIS IS A STRING" >>> str.isupper() True >>> str = "this is a string" >>> str.istitle() False >>> str = "This Is A String" >>> str.istitle() True >>>

    字符串的格式化输出 对于字符串的格式化输出,我这里不做解释了,只进行以下示例:常用的有%s,%d,%f,如果需要其他的请通过谷歌查询

    >>> "I like a %s" % "girl" 'I like a girl' >>> "%d years old" % 20 '20 years old' >>> "PI is %f" % 3.1415926 'PI is 3.141593' >>>

    好了,以上包括上一篇就是对于字符串的所有讲解,希望同学用动起你们的手指和用起来你们聪明的大脑。如有不足之处请指教。

    转载请注明原文地址: https://ju.6miu.com/read-670704.html

    最新回复(0)