Python字符串操作的示例

  介绍

这篇文章给大家分享的是有关Python字符串操作的示例的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

今天给大家总结一下字符串的所有操作,字符串替换,删除,截取,复制,连接,比较,查找,包含,大小写转换,分割等。

<强>去空格及特殊符号

s.strip () .lstrip () .rstrip (& # 39; & # 39;)

<强>复制字符串

#拷贝字符串(sStr1 sStr2)   时间=sStr1  & # 39;拷贝字符串# 39;   sStr2 =sStr1   时间=sStr1  & # 39; strcpy2& # 39;   print  sStr2

<强>连接字符串

# strcat (sStr1 sStr2)   时间=sStr1  & # 39; strcat # 39;   时间=sStr2  & # 39;添加# 39;   sStr1  +=sStr2   print  sStr1

<强>查找字符

# strchr (sStr1 sStr2)   #,& lt;, 0,为未找到   时间=sStr1  & # 39; strchr& # 39;   时间=sStr2  & # 39; & # 39;   时间=nPos  sStr1.index (sStr2)   print 非营利组织

<强>比较字符串

# strcmp (sStr1 sStr2)   时间=sStr1  & # 39; strchr& # 39;   时间=sStr2  & # 39; strch& # 39;   print  cmp (sStr1 sStr2)

<强>扫描字符串是否包含指定的字符

# strspn (sStr1 sStr2)   时间=sStr1  & # 39; 12345678 & # 39;   时间=sStr2  & # 39; 456 & # 39;   # sStr1 以及chars  both  sStr1 以及sStr2拷贝   print  len (sStr1 以及sStr2)

<强>字符串长度

# strlen (sStr1)   时间=sStr1  & # 39; strlen # 39;   print  len (sStr1)

<强>将字符串中的大小写转换

# strlwr (sStr1)   时间=sStr1  & # 39; JCstrlwr& # 39;   时间=sStr1  sStr1.upper ()   # sStr1 =, sStr1.lower ()   print  sStr1

<强>追加指定长度的字符串

# strncat (sStr2 sStr1, n)   时间=sStr1  & # 39; 12345 & # 39;   时间=sStr2  & # 39;六边形abcdef # 39;   时间=n  3   sStr1  +=, sStr2 [0: n]   print  sStr1

<强>字符串指定长度比较

# strncmp (sStr2 sStr1, n)   时间=sStr1  & # 39; 12345 & # 39;   时间=sStr2  & # 39; 123 bc # 39;   时间=n  3   print  cmp (sStr1 [0: n], sStr2 [0: n])

<强>复制指定长度的字符

# strncpy (sStr2 sStr1, n)   时间=sStr1  & # 39; & # 39;   时间=sStr2  & # 39; 12345 & # 39;   时间=n  3   sStr1 =, sStr2 [0: n]   print  sStr1

<强>将字符串前n个字符替换为指定的字符

# strnset (sStr1, ch, n)   时间=sStr1  & # 39; 12345 & # 39;   时间=ch  & # 39; " # 39;   时间=n  3   时间=sStr1  n  *, ch  +, sStr1 [3:]   print  sStr1

<强>扫描字符串

# strpbrk (sStr1 sStr2)   时间=sStr1  & # 39; cekjgdklab& # 39;   时间=sStr2  & # 39; gka& # 39;   nPos =1   for  c  sStr1拷贝:   ,,,if  c 拷贝sStr2:   ,,,,,,,nPos =, sStr1.index (c)   ,,,,,,,休息   print 非营利组织

<强>翻转字符串

# strrev (sStr1)   时间=sStr1  & # 39;英语字母# 39;   时间=sStr1  sStr1 [:: 1)   print  sStr1

<强>查找字符串

# strstr (sStr1 sStr2)   时间=sStr1  & # 39;英语字母# 39;   时间=sStr2  & # 39; cde # 39;   print  sStr1.find (sStr2)

<强>分割字符串

# strtok (sStr1 sStr2)   时间=sStr1  & # 39; ab, cde, fgh, ijk& # 39;   时间=sStr2  & # 39; & # 39;   时间=sStr1  sStr1 [sStr1.find (sStr2), +, 1:]   print  sStr1   #或者   时间=s  & # 39; ab, cde, fgh, ijk& # 39;   print (s.split (& # 39; & # 39;))

<强>连接字符串

delimiter =, & # 39; & # 39;   mylist =,(& # 39;巴西# 39;,,& # 39;俄罗斯# 39;,,& # 39;印度# 39;,,& # 39;中国# 39;】   print  delimiter.join (mylist)   PHP 中,addslashes 的实现   def  addslashes (s):   ,,,d =, {& # 39;“& # 39;: & # 39; \ \“& # 39;,,“& # 39;“:“\ \ & # 39;“,,“\ 0“:“\ \ \ 0“,,“\ \”:“\ \ \ \“}   ,,,return  & # 39; & # 39; . join (d.get (c, c), for  c 拷贝)   时间=s “John  & # 39;约翰尼# 39;,Doe (又称为只\“Super 乔\“)\ \ \ 0“;   print 年代   print  addslashes (s)

Python字符串操作的示例