String Operations - Python Programming by Example (2015)

Python Programming by Example (2015)

7. String Operations

This chapter explains how to work with String operation in Python.

7.1 Getting Started

We already use string as data type, https://docs.python.org/3/library/string.html . In this section, we explore some operations in string.

The next step is to explore how to work with string.

7.2 Concatenating Strings

If you have a list of string, you can concatenate into one string. You can use + operator and format() function. Here is a sample code

# Concatenating

print(str1 + " " + str2)

print(str1, str2)

print("%s %s" % (str1, str2))

print("{} {}".format(str1, str2))

7.3 String To Numeric

Sometime you want to do math operations but input data has string type. To convert string type into numeric, you can use int() for String to Integer and float() for string to Float.

The following is a sample code to implement string to numeric conversion.

# string to numeric

a = "2"

b = "6.8"

num1 = int(a)

num2 = float(b)

print(num1)

print(num2)

7.4 Numeric to String

It is easy to convert numeric to String type, you can use str(). You can get string type automatically.

# numeric to string

a = 6

b = 8.56

str1 = str(a)

str2 = str(b)

print(str1)

print(str2)

7.5 String Parser

The simple solution to parsing String uses split() with delimiter parameter. For example, you have String data with ; delimiter and want to parse it. Here is sample code

# parsing

msg = 'Berlin;Amsterdam;London;Tokyo'

cities = msg.split(';')

for city in cities:

print(city)

7.6 Check String Data Length

You can use len() to get the length of data.

msg = 'Hello world, Python!'

# get a length of string

length = len(msg)

print(length)

7.7 Copy Data

You may copy some characters from String data. To do it, you can use [start:end] syntax. Here is syntax format:

msg = 'Hello world, Python!'

# copy

print(msg[5:])

print(msg[:5])

print(msg[-3:])

print(msg[:-3])

print(msg[2:6])

print(msg[5:8])

7.8 Upper and Lower Case Characters

In some situation, you want to get all string data in upper or lower case characters. This feature is built in String object. upper() function is used to make whole string in upper case and lower() is used to make whole string in lower case.

The following is a sample code to get upper and lower case characters.

msg = 'Hello world, Python!'

# upper & lower

print(msg.upper())

print(msg.lower())

7.9 Testing A Program

We can write our code in ch17_01.py completely as follows.

str1 = "hello world"

str2 = "python"

# Concatenating

print(str1 + " " + str2)

print(str1, str2)

print("%s %s" % (str1, str2))

print("{} {}".format(str1, str2))

# string to numeric

a = "2"

b = "6.8"

num1 = int(a)

num2 = float(b)

print(num1)

print(num2)

# numeric to string

a = 6

b = 8.56

str1 = str(a)

str2 = str(b)

print(str1)

print(str2)

# parsing

msg = 'Berlin;Amsterdam;London;Tokyo'

cities = msg.split(';')

for city in cities:

print(city)

# string operations

msg = 'Hello world, Python!'

# upper & lower

print(msg.upper())

print(msg.lower())

# copy

print(msg[5:])

print(msg[:5])

print(msg[-3:])

print(msg[:-3])

print(msg[2:6])

print(msg[5:8])

# get a length of string

length = len(msg)

print(length)

Save the script and run the program.

$ python3 ch07_01.py

Program output:p7-1