lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

[python3]:  string -  在‘字符串s1’中插入‘字符串s2’

 

 

 

 

一、基本说明

 

 

  0、  【python ‘字符串的变量’】:

 

    0.0、  python字符串变量具有‘只读’属性;python字符串变量的切片,遵循原则【前闭后开】  

      0.0.1、  python中‘字符串的变量’,是‘只读’变量;不能改变‘字符串变量’局部的数值。

      0.0.2、  python中的字符串变量的‘切片’,遵循原则‘前闭后开’。  ‘前闭后开’原则的内容:包含‘此切片’的‘开始索引处的字符’,不包含‘此切片’的‘结尾索引’处的字符。

 

    0.1、  举例: msg = 'hello_world'

 

    0.2、  说明:  msg是‘只读’变量,不能改变‘msg局部的数值’。 不允许这样做:msg[1] = '33'

 

    0.3、  违规的操作:  msg[1]  =  '33'

 

    0.4、  【前闭后开】原则:  切片,包含开头字符且不包含结尾的字符。 say = 'world'

      0.4.1、  say[1:3]:    这个‘切片’的值是‘or’;  即切片say[1:3]:    包含‘say[1]='o', say[2]='r'’,不包含'say[3]='l' '。

 

 

  1、【在字符串中插入字符串的核心原理 】:  倒数计数(从字符串的尾部开始计数,到字符串的头部结束计数)

 

    1.0、  new_string  =  s1[ :-n]  +  s2  +  s1[-n:] 

      1.0.1、  说明:s1[:-n]:  从s1开始位置输出,到s1[-n]位置结束。

      1.0.2、  说明:s1[-n:]:  从s1[-n]位置开始输出,到s1末尾结束。

      1.0.3、  【-n:倒数计数】:  -n是s1中字符串的索引, 从末尾开始计数:-1,-2,...,-n

      1.0.4、  【n:n的数值】:  n=len(string);  【n】的数值,是字符串string中的元素个数。

 

    1.1、  【举例说明】:  原始字符串: s1 = 'hello',  s2='52'

      1.0.0、【s1='hello':倒数计数】:n=len(s1)=5

        --        h        e        l        l        o

        --        -5       -4       -3      -2     -1

                 1.0.1、  在'字符串s1'的【h】之后和【e】之前的位置插入'字符串s2',n = -4

      1.0.2、  new_s  =  s1[ : -4 ]   +   s2   +   s1[ -4 : ]

      1.0.3、  new_s  =  'h52ello'

 

 

  2、重要说明:  

 

    3.1、  【字符串】:  从‘big.txt’中读取的内容,是一个‘字符串’的列表;

 

    3.2、  【列表的元素】:  列表的元素,是‘文件big.txt’的‘行’;

 

    3.3、  【行】:  ‘行’中包含换行符‘\n’; 每行的‘内容’以字符‘\n’结尾; 我删除‘行尾’的‘\n’的方法: line_string.rstrip('\n')

 

 

 

 

二、源代码和数据文件

 

 

  1、  程序源文件:pytest

 1 #!/usr/bin/python3
 2 
 3 
 4 
 5 
 6 '''
 7   file_name = pytest
 8   python3_version = 3.11.8
 9   author = lnlidawei
10   date= 20240303
11 '''
12 
13 
14 
15 
16 #  open file
17 fh1 = open('big.txt')
18 
19 
20 #  get data from file
21 dataset = fh1.readlines()
22 
23 
24 #  close fh1
25 fh1.close()
26 
27 
28 #  loop dataset
29 def loop_data(ds):
30     print(f"\n[loop_data]:\tbegin\n")
31     ln = 0
32     for line  in ds:
33         print(f"\t[line_{ln}]:\t{line}")
34         ln = ln + 1
35     print(f"[loop_data]:\tend\n")
36 
37 
38 #  loop_data(dataset)
39 #  print(f"\n[all]:\t{dataset} \n")
40 
41 
42 
43 
44 #  save data to file='save.txt'
45 fh2 = open('save.txt', 'w+')
46 
47 
48 #  loop 'dataset' and add some data to that lines.
49 num = 0
50 for line in dataset:
51     tmp = ' [ ' + str(num) + ' ] '
52     #print(tmp)
53     num = num + 1
54     new_line = line[:-2] + tmp + line[-2:]
55     print(f"[new_line]:\t{new_line}")
56 
57     fh2.write(new_line)
58 
59 num = -1
60 
61 
62 #  close fh2
63 fh2.close()

 

 

  2、  数据文件:big.txt

hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.
hello this name is big one.

 

 

 

 

三、运行结果

 

  1、pytest的运行结果

 1 [wit@fedora tmp]$ ./pytest 
 2 [new_line]:     hello this name is big one [ 0 ] .
 3 
 4 [new_line]:     hello this name is big one [ 1 ] .
 5 
 6 [new_line]:     hello this name is big one [ 2 ] .
 7 
 8 [new_line]:     hello this name is big one [ 3 ] .
 9 
10 [new_line]:     hello this name is big one [ 4 ] .
11 
12 [new_line]:     hello this name is big one [ 5 ] .
13 
14 [new_line]:     hello this name is big one [ 6 ] .
15 
16 [new_line]:     hello this name is big one [ 7 ] .
17 
18 [new_line]:     hello this name is big one [ 8 ] .
19 
20 [new_line]:     hello this name is big one [ 9 ] .
21 
22 [new_line]:     hello this name is big one [ 10 ] .
23 
24 [new_line]:     hello this name is big one [ 11 ] .
25 
26 [new_line]:     hello this name is big one [ 12 ] .
27 
28 [new_line]:     hello this name is big one [ 13 ] .
29 
30 [new_line]:     hello this name is big one [ 14 ] .
31 
32 [new_line]:     hello this name is big one [ 15 ] .
33 
34 [new_line]:     hello this name is big one [ 16 ] .
35 
36 [new_line]:     hello this name is big one [ 17 ] .
37 
38 [new_line]:     hello this name is big one [ 18 ] .
39 
40 [new_line]:     hello this name is big one [ 19 ] .
41 
42 [new_line]:     hello this name is big one [ 20 ] .
43 
44 [new_line]:     hello this name is big one [ 21 ] .
45 
46 [new_line]:     hello this name is big one [ 22 ] .
47 
48 [new_line]:     hello this name is big one [ 23 ] .
49 
50 [new_line]:     hello this name is big one [ 24 ] .
51 
52 [new_line]:     hello this name is big one [ 25 ] .
53 
54 [new_line]:     hello this name is big one [ 26 ] .
55 
56 [new_line]:     hello this name is big one [ 27 ] .
57 
58 [new_line]:     hello this name is big one [ 28 ] .
59 
60 [new_line]:     hello this name is big one [ 29 ] .
61 
62 [new_line]:     hello this name is big one [ 30 ] .
63 
64 [new_line]:     hello this name is big one [ 31 ] .
65 
66 [new_line]:     hello this name is big one [ 32 ] .
67 
68 [new_line]:     hello this name is big one [ 33 ] .
69 
70 [new_line]:     hello this name is big one [ 34 ] .
71 
72 [wit@fedora tmp]$ 

 

  2、生成的新文件:

 1 [wit@fedora tmp]$ cat save.txt
 2 hello this name is big one [ 0 ] .
 3 hello this name is big one [ 1 ] .
 4 hello this name is big one [ 2 ] .
 5 hello this name is big one [ 3 ] .
 6 hello this name is big one [ 4 ] .
 7 hello this name is big one [ 5 ] .
 8 hello this name is big one [ 6 ] .
 9 hello this name is big one [ 7 ] .
10 hello this name is big one [ 8 ] .
11 hello this name is big one [ 9 ] .
12 hello this name is big one [ 10 ] .
13 hello this name is big one [ 11 ] .
14 hello this name is big one [ 12 ] .
15 hello this name is big one [ 13 ] .
16 hello this name is big one [ 14 ] .
17 hello this name is big one [ 15 ] .
18 hello this name is big one [ 16 ] .
19 hello this name is big one [ 17 ] .
20 hello this name is big one [ 18 ] .
21 hello this name is big one [ 19 ] .
22 hello this name is big one [ 20 ] .
23 hello this name is big one [ 21 ] .
24 hello this name is big one [ 22 ] .
25 hello this name is big one [ 23 ] .
26 hello this name is big one [ 24 ] .
27 hello this name is big one [ 25 ] .
28 hello this name is big one [ 26 ] .
29 hello this name is big one [ 27 ] .
30 hello this name is big one [ 28 ] .
31 hello this name is big one [ 29 ] .
32 hello this name is big one [ 30 ] .
33 hello this name is big one [ 31 ] .
34 hello this name is big one [ 32 ] .
35 hello this name is big one [ 33 ] .
36 hello this name is big one [ 34 ] .
37 [wit@fedora tmp]$ 
38 [wit@fedora tmp]$ 

 

 

 

四、参考资料

 

  1、  python3字符串|菜鸟教程   -  https://www.runoob.com/python3/python3-string.html

 

posted on 2024-03-03 02:14  lnlidawei  阅读(43)  评论(0编辑  收藏  举报