Python的序列类型——字符串

一、字符串的误解

         计算机系统的每个内存单元都是唯一并且连续的物理地址,字符串在内存中一旦创建就被操作系统分配一块唯一并且连续的地址。计算机系统不允许我们修改字符串中的内容,一旦我想试图进行修改,系统马上报错。但如果我们想修改其中的内容,计算机系统会为我们开辟一块新的内存空间。 

例如:newstring是新生成的内存

oldstring = “周杰伦的烟花易冷”
newstring = oldstring. replace(‘周杰伦’, ’林志炫’)

例如:string是新生成的内存

string_1 = "abc"
string _2 = "def"
string = string _1 + string _2

注:上面两个例子,oldstring 和string 的值并没有改变,string的意示图如下:

 

 

二、几个常用的函数

1. join(...)函数

参数...为要连接的元素序列,该方法常用于往字符串中间插入字符串。用法如下:

oldstring = "成熟是一种明亮而不刺眼的光辉"
newstring = " ".join(oldstring)
print(newstring)  #输出:成 熟 是 一 种 亮 而 不 刺 眼 的 光 辉

 

2. split(...)函数

 将参数作为分隔符,可指定分隔的次数,但自己匹配的参数无法出现才结果集中,该函数常用与

不需要自己匹配的参数的运算中,用法如下:

string = "5+10"
value_1,value_2 = string.split('+')
value = int(value_1) + int(value_2)
print(value)    #输出:15

 

3. maketrans函数 替换函数

maketrans(x, y=None, z=None, /)  、 translate(...)  和 replace()。maketrans函数用于建立键值对的映射,而替换函数translate函数和replace函数的使用各有千秋。下面将用一个例子来说明的他们的作用。

例如:使maketrans 与 translate的结合

string = "abcdefghijk"
setKY = str.maketrans("abcd","----") #Set built-in
print(string.translate(setKY))  #输出:----efghijk    

例如:replace的使用

string = "abcdabcdabcdabcd"
print(string.replace("ab",'--',2))  #参数2表示替换目标序列的前两个子序列
print(string.replace("ac",'--',2))  #替换无效,还是原样输出

 

4. expandtabs(...)函数

   使用该函数将指定的n个字符为单位,当字符对象不足n个字符时,自动为对象字符填充剩余的空格。例如:

test = “username\temail\tpassword\nLynnLee\t nLynnLee.@qq.com\t123\n nLynnLee\t nLynnLee.@qq.com\t123\n”
v = test.expandtabs(20)
print(v)
结果为:
username            email               password
LynnLee             nLynnLee.@qq.com    123
nLynnLee            nLynnLee.@qq.com    123

 

三、其他常用函数总结

 

 

 

转载于:https://www.cnblogs.com/Lynnblog/p/8903061.html

### Python 字符串处理方法和函数 #### 字符串定义 字符串Python 中最常用的数据类型之一,它们可以用单引号或双引号来表示[^1]。 ```python single_quoted_string = 'Hello, world!' double_quoted_string = "Hello, universe!" ``` #### 基本字符串操作函数 ##### 拼接字符串 可以通过加号 `+` 来实现两个字符串的拼接: ```python greeting = "Hello" name = "Alice" message = greeting + ", " + name # 输出: Hello, Alice ``` ##### 分割字符串 使用 `split()` 函数可以根据指定分隔符将字符串分割成列: ```python text = "apple,banana,cherry" fruits = text.split(",") # ['apple', 'banana', 'cherry'] ``` ##### 查找子串位置 利用 `find()` 或者 `index()` 可以找到某个子串首次出现的位置;如果找不到,则 `find()` 返回 `-1` 而 `index()` 抛出异常: ```python sentence = "Welcome to the jungle." position_find = sentence.find("jungle") # position_find=11 try: position_index = sentence.index("desert") except ValueError as e: print(e) # substring not found ``` ##### 替换子串 通过 `replace(old, new)` 将旧子串替换成新子串: ```python old_text = "I like cats and dogs." new_text = old_text.replace("cats", "rabbits") # I like rabbits and dogs. ``` ##### 大小写转换 支持多种大小写的转换方式,比如全部大写、首字母大写等: ```python lowercase = "hello".upper() # HELLO uppercase = "WORLD".lower() # world capitalized = "john doe".capitalize() # John doe titlecased = "john DOE".title() # John Doe ``` #### 特殊字符处理 ##### 去除空白字符 为了去除字符串两端多余的空格或其他空白字符,可以采用 `strip()` 方法。此方法会移除开头和结尾处所有的空白字符包括空格、制符 `\t` 和换行符 `\n`),但不会影响中间部分的任何空白字符[^2]。 ```python trimmed_string = ' hello there! '.strip() print(trimmed_string) # hello there! ``` #### 序列字符串 当有一个由多个项组成的序列并希望将其组合成单一字符串时,可借助于 `join()` 方法。该方法接收一个迭代器作为参数,并用给定的分隔符把各个元素连在一起形成一个新的字符串对象[^3]。 ```python words_list = ["Python", "is", "awesome"] joined_sentence = "-".join(words_list) print(joined_sentence) # Python-is-awesome ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值