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