中的字符串拼接和遍历操作

字符串管理在超越56%编制程序语言中都以重头戏,AHK 自然也不例外。AHK
在字符串管理地点依然很利索的,假若明白好了,写起代码来会一语双关。本文先介绍最常用的操作:拼接和遍历。

目录:

官方澳门新永利下载 1

五.字符串.png

东拼西凑可能是最常用的操作了,在 AHK
中也极度轻巧,直接将八个字符串用空格隔开分离就可以了。

字符串

  • 字符串在py3中,字符串是UNICODE的类别,在PY第22中学是byte类别
  • 字符串str 与元组一样是不可变的
  • 字符串能够用单引号,双引号,三单引号,四双引号定义,效果是一律的,因为在py中,单引号与双引号的效劳是一致的

name = "dailiang"
print(name,type(name))

dailiang <class 'str'>

因为字符串是线性结构,[官方澳门新永利下载,切开、封包、解包 (线性结构的特定成效)] 、迭代
、 成员运算符 在字符串都足以用

a := "abc"b := 100c := "def"d := 123.456e := 200; f 的值是字符串 abc100anddef123.456f := a b "and" c d; g 的值是字符串 100200g := b e

目录操作

x = "abc"
x[0]

'a'

任凭是字符串、整数仍旧浮点数,都得以用空格拼接成一个字符串。更玄妙的是,空格能够和任何算术运算符一齐使用。

迭代

怎样叫可迭代对象?能够遍历的都称呼可迭代对象
若果是可迭代对象都能够迭代,举个例子集结set

name = "dai"
for i in name :
    print(i)

d
a
i
a := 100b := 200; c 的值是 100150c := a b - 50

切片

name = "dailiang"
name[0:2]

'da'

name = "dai"
name[::-1]

'iad'

那么问题来了,字符串不是不可变吗?为什么可以反转?? 

name = "dai"
name[::-1]
print(name)

dai

我们发现字符串经过[::-1] 反转后,依然没有发生改变,但是反转的结果我们可以通过另外的变量接收

name = "dai"
b = name[::-1]
print(b)

iad

元组也是不可变的,与字符串原理一样的

a = (1,3,4)
b = a[::-1]
print(a)
print(b)

(1, 3, 4)
(4, 3, 1)

字符串与元组一样是不可变的,所以不能通过索引(下标修改)

name = "dailiang"
name[1] = "h"  #'str' object does not support item assignment

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-14-99dc62f6cda4> in <module>()
      1 name = "dailiang"
----> 2 name[1] = "h"  #'str' object does not support item assignment


TypeError: 'str' object does not support item assignment

实际上 AHK 中整数 100200 和 字符串 “100200”
是未有区分的,当子弹头用的时候正是 100200,当字符串用的时候正是”100200″,当然内部贯彻上会有部分优化。假设认为无法接受的话,也得以感觉整数和字符串是有分别的,在实际上利用中会自动相互调换。

分子运算符 in 与 not in

成员运算符与迭代一样,都不算是线性结构的特性

name = "dailiangk"
if "i" in name :
    print("yes")
else:
    print("no")

yes

if "i" not in name :
    print("yes")
else:
    print("no")

no

其实,AHK 中 .是专程用来拼接字符串的,在一些意况依旧无法轻易的。

打包与解封装:

name = "dailiang"
a,*b,c = name 
print(a,b,c)

d ['a', 'i', 'l', 'i', 'a', 'n'] g

笔者们开采b照旧是列表

a := 300b := 200; c 的值是 100c := a - b; d 的值是 300-200d := a . - b

字符串的形式之转义

a = "anbc"
print(a)
b = "aataa"
print(b)

a
bc
aa  aa

a = "a\bc"
print(a)

abc

代表转义,n 代表换行 ,t 代表 tab 键, “” 代表

在Python中字符串的单引号与双引号都无法儿收回特殊字符的转义,要是想收回引号内具备字符的转义,须要在引号后边加r

a = r"anbct"
print(a)

anbct

正因为空格能够和其余算术运算符一齐使用,使用起来能够很利索,为了制止歧义,空格唯有在必要的状态(即那些表达式没有其它的表达形式时)才起拼接功效。

字符串的分开操作

再有四个丰盛常见的操作,是遍历一个字符串的子字符串。Loop, Parse
是特意用来做那么些的。

细分操作之split

help(str.split)

Help on method_descriptor:

split(...)
    S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.

看上面包车型大巴讲授: Return a list of the words in
S,会回去叁个列表的形式
字符串被split分割后,重返的是列表
假设split什么参数也不加,会暗中同意依照空格来划分

name = "my name is dailiang"
b = name.split()
print(b)

['my', 'name', 'is', 'dailiang']

钦命分割字符

name = "my name is dailiang"
name.split(sep="i")

['my name ', 's da', 'l', 'ang']

不移至理你可以毫不写sep= 来定义分割字符

name = "my name is dailiang"
name.split("i")

['my name ', 's da', 'l', 'ang']

分割字符在字符串被细分后遗弃

点名分割次数

name = "my name is dailiang"
name.split("i",1)

['my name ', 's dailiang']
Loop, Parse, InputVar [, Delimiters, OmitChars]

分割操作之rsplit

help(str.rsplit)

Help on method_descriptor:

rsplit(...)
    S.rsplit(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string, starting at the end of the string and
    working to the front.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified, any whitespace string
    is a separator.

starting at the end of the string and working to the front

c = "my name is dailiang"
d = c.rsplit("a",1)
print(d)

['my name is daili', 'ng']

里头 InputVar 是变量名,无需加 %。Delimiters 是分隔符,OmitChars
是要不经意的字符。

分开操作之split实战

作者们在运行职业中时常遇上下边的布置
hostname = 127.0.0.1
方今大家要把 127.0.0.1 抽出来 ,应该如何做?
唤醒:split 重返值是列表格式,列表能够做解封装操作

a = "hostname=127.0.0.1 "
a.split("=")
e,f = a.split("=")  #这是解包操作
print(f)

 127.0.0.1 

最轻便易行的气象正是遍历字符串中的每三个字符。

分割操作之splitlines 分割多行

help(str.splitlines)

Help on method_descriptor:

splitlines(...)
    S.splitlines([keepends]) -> list of strings

    Return a list of the lines in S, breaking at line boundaries.
    Line breaks are not included in the resulting list unless keepends
    is given and true.

song = """when i was young 
i'd listen to the radio 
wating for my favourite songs 
"""  #格式化输出
print(song,type(song))

when i was young 
i'd listen to the radio 
wating for my favourite songs 
 <class 'str'>

song.splitlines()

['when i was young ',
 "i'd listen to the radio ",
 'wating for my favourite songs ']

Line breaks are not included in the resulting list unless keepends
is given and true. 然则这么未有保留换行符如何做?

b = song.splitlines(True)
print(b)

['when i was young n', "i'd listen to the radio n", 'wating for my favourite songs n']
text := "abcd"Loop, Parse, text{ ; 依次弹出 ; a ; b ; c ; d MsgBox, %A_LoopField%}

细分操作之partition

help(str.partition)

Help on method_descriptor:

partition(...)
    S.partition(sep) -> (head, sep, tail)

    Search for the separator sep in S, and return the part before it,
    the separator itself, and the part after it.  If the separator is not
    found, return S and two empty strings.

翻译:
查找分割符,再次来到分割符后边的故事情节、分隔符自个儿、分隔符前面包车型客车原委。假设分隔符未有找到,把字符串本人重回,并再次来到八个空串,以元组的款型

s = "dailiang"
s.partition("a")

('d', 'a', 'iliang')

s = "dailiang"
s.partition("k")

('dailiang', '', '')

s = "dailiang"
s.partition()

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-85-af43fdf69b3b> in <module>()
      1 s = "dailiang"
----> 2 s.partition()


TypeError: partition() takes exactly one argument (0 given)

partition 是回来三个带有 八个 成分的元组,不能接null,可是能够接空格

s = "dailiang"
s.partition(" ")

('dailiang', '', '')

Delimiters 和 OmitChars 都简短的状态,就是遍历字符串中的每八个字符。

分开操作之rpartition

rpartition 是从右至左分割

a = "efg h i"
b = a.rpartition(" ")
print(b)

('efg h', ' ', 'i')

发表评论

电子邮件地址不会被公开。 必填项已用*标注