Python

 

 

 

 

Python - String Operation/String Manipulation 

 

If you are new to Computer language. The word 'String' would not sound clear to you.  Simply put, a String in a computer language is 'multiple characters concatenated (combined) into a text'.

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

  • String Declaration/Assignment and print - Example 1
  • String Concatenation - Example 2
  • Finding Length of a String - Example 3
  • Cut out one character from a String - Example 4
  • Cut out a segment from a String - Example 5
  • Convert all the letters to Upper Case - Example 6
  • Convert all the letters to Lower Case - Example 7
  • Compare two strings - Example 8
  • Split a String with a delimiter - Example 9
  • Find a substring in a string - Example 10
  • Converting a number to a string - Example 11
  • Writing double quotes in a string - Example 12
  • Converting Binary String to String, Converting a String to Binary String - Example 13
  • Replace a substring in a String - Example 14
  • Removing all the spaces in a String - Example 15
  • Removing Carriage Return in a String - Example 16
  • Convert a Hex String into a stream of Hex Numbers(binascii package) - Example 17
  • Convert a stream of Hex Numbers into a Byte Array(binascii package) - Example 18
  • Trimming a string - Example 19

 

Examples :

 

< 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 >