Python_String

字符串操作/字符串处理示例:

  1. 字符串声明/赋值和打印 - 示例1
  2. 字符串拼接 - 示例2
  3. 查找字符串长度 - 示例3
  4. 从字符串中剪切一个字符 - 示例4
  5. 从字符串中剪切一段 - 示例5
  6. 将所有字母转换为大写 - 示例6
  7. 将所有字母转换为小写 - 示例7
  8. 比较两个字符串 - 示例8
  9. 使用分隔符拆分字符串 - 示例9
  10. 在字符串中查找子字符串 - 示例10
  11. 将数字转换为字符串 - 示例11
  12. 在字符串中写入双引号 - 示例12
  13. 将二进制字符串转换为字符串,将字符串转换为二进制字符串 - 示例13
  14. 替换字符串中的子字符串 - 示例14
  15. 删除字符串中的所有空格 - 示例15
  16. 删除字符串中的回车符 - 示例16
  17. 将十六进制字符串转换为十六进制数字流(binascii包)- 示例17
  18. 将十六进制数字流转换为字节数组(binascii包)- 示例18
  19. 修剪字符串 - 示例19
< Example 1 > String Declaration/Assignment and print

 

aString = "Hello World"

print(aString)

 

Result :----------------------------------

Hello World

 

 

< Example 2 > String Concatenation

 

aString = "Hello World"

bString = " Python"

cString = aString + bString

print(cString)

 

Result :----------------------------------

Hello World Python

 

 

< Example 3 > Finding Length of a String

 

aString = "Hello World"

print(len(aString))

 

Result :----------------------------------

13

 

 

< Example 4 > Cut out one character from a String

 

aString = "Hello World"

print(aString[0])

 

Result :----------------------------------

H

 

 

< Example 5 > Cut out a segment from a String

 

aString = "Hello World"

print(aString[1:5])

 

Result :----------------------------------

ello

 

 

< Example 6 > Convert all the letters to Upper Case

 

aString = "Hello World"

print(aString.upper())

 

Result :----------------------------------

HELLO WORLD

 

 

< Example 7 > Convert all the letters to Lower Case

 

aString = "Hello World"

print(aString.lower())

 

Result :----------------------------------

hello world

 

 

< Example 8 > Compare two strings

 

aString = "Hello"

bString = "hello"

 

if aString == bString :

   print("Two Strings are same.")

else :

   print("Two Strings are different.")

 

if aString.upper() == bString.upper() :

   print("Two Strings are same.")

else :

   print("Two Strings are different.")

 

Result :----------------------------------

Two Strings are different.

Two Strings are same.

 

 

< Example 9 > Split a String with a delimiter

 

aString = "This is an example of string operations in Python"

splitString = aString.split(" ")

 

print(splitString)

print("The 4th Word = ",splitString[3])

 

Result :----------------------------------

['This', 'is', 'an', 'example', 'of', 'string', 'operations', 'in', 'Python']

The 4th Word =  example

 

 

< Example 10 > Find a substring in a string

 

aString = "This is a Python string"

 

print(aString.find("This"))

print(aString.find("string"))

print(aString.find("java"))

 

Result :----------------------------------

0

17

-1

 

 

< Example 11 > Converting a number to a string

 

num1 = 1234

num2 = 5678

 

str1 = str(num1)

str2 = str(num2)

 

stradd = str1+str2

numadd = num1+num2

 

print(numadd)

print(stradd)

 

Result :----------------------------------

6912

12345678

 

 

< Example 12 > Writing double quotes in a string

# there are several different ways to writing double quotes in a string

 

aString = 'He said "Hello World"'

bString = "He said \"Hello World\""

 

print("aString =",aString)

print("bString =",bString)

 

Result :----------------------------------

aString = He said "Hello World"

bString = He said "Hello World"

 

 

< Example 13 > Converting Binary String to String, Converting a String to Binary String

 

aString = 'Hello World'

bString = b'Hello World'

 

print("aString =",aString)

print("bString =",bString)

print("aString.encode('ascii') =",aString.encode('ascii'))

print("bString.decode('ascii') =",bString.decode('ascii'))

 

Result :----------------------------------

aString = Hello World

bString = b'Hello World'

aString.encode('ascii') = b'Hello World'

bString.decode('ascii') = Hello World

 

 

< Example 14 > Replace a substring in a String

 

orgStr = 'Hello World'

replacedString = orgStr.replace('Hello', 'Hi')

 

print("orgStr = ", orgStr)

print("replacedString = ", replacedString)

 

Result :----------------------------------

orgStr =  Hello World

replacedString =  Hi World

 

 

< Example 15 > Removing all the spaces in a String

 

orgStr = 'H e l l o W o r l d'

replacedString = orgStr.replace(" ", "")

 

print("orgStr = ", orgStr)

print("replacedString = ", replacedString)

 

Result :----------------------------------

orgStr =  H e l l o W o r l d

replacedString =  HelloWorld

 

 

< Example 16 > Removing Carriage Return in a String

 

orgStr = '\nHello\nWorld'

replacedString = orgStr.replace("\n", " ")

 

print("orgStr = ", orgStr)

print("replacedString =", replacedString)

 

Result :----------------------------------

orgStr =  

Hello

World

replacedString =  Hello World

 

 

< Example 17 > Convert a Hex String into a stream of Hex Numbers (binascii package)

 

import binascii

 

hexStr = "AABBCC"

hexNumbers = binascii.a2b_hex(hexStr)

 

print("hexStr = ",hexStr)

print("hexNumbers = ", hexNumbers)

 

Result :----------------------------------

hexStr =  AABBCC

hexNumbers =  b'\xaa\xbb\xcc'

 

 

< Example 18 > Convert a stream of Hex Numbers into a Byte Array (binascii package)

 

import binascii

 

hexStr = "AABBCC"

hexNumbers = binascii.a2b_hex(hexStr)

byteArray = bytearray(hexNumbers)

 

print("hexStr = ",hexStr)

print("hexNumbers = ", hexNumbers)

print("byteArray = ", byteArray)

print("hexStr[0]=",hexStr[0])

print("hexNumbers[0]=",hexNumbers[0])

print("byteArray[0]=",byteArray[0])

 

Result :----------------------------------

hexStr =  AABBCC

hexNumbers =  b'\xaa\xbb\xcc'

byteArray =  bytearray(b'\xaa\xbb\xcc')

hexStr[0]= A

hexNumbers[0]= 170

byteArray[0]= 170

 

 

< Example 19 > Trimming a string

 

strA = "   Hello World          "

strB = "### Hello World #####"

trimmedA = strA.strip(" ")

trimmedB = strA.strip("#")

 

print("strA =",strA)

print("trimmedA =",trimmedA)

print("strB =",strB)

print("trimmedB =",trimmedB)

 

Result :----------------------------------

strA =    Hello World          

trimmedA = Hello World

strB = ### Hello World #####

trimmedB =    Hello World   

 

 

< Example 20 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值