Strings in “Python” programming are sequences of characters enclosed within single quotes (' '), double quotes (" "), or triple quotes (''' '''). They are used to store text data and are immutable, meaning they cannot be changed once created. Here’s an example of creating a string in Python:

# Creating a string
message = "Hello, World!"
print(message)  # Output: Hello, World!
Hello, World!

1 String Manipulation

You can access individual characters in a string using the index, where each character is assigned a position starting from 0.

# Accessing individual characters in a string
word = "Python"
print(word[0])  # Output: P
P

String slicing allows you to extract a portion of a string by specifying a range of indices.

# String slicing

sentence = "You are great!"

# Print a substring of the original sentence from index 4 to 9 (exclusive)
print(sentence[4:9])  # Output: are g

# Print a substring of the original sentence from index 4 to 9 (exclusive) with a step of 2
print(sentence[4:9:2])  # Output: aeg

# Print a substring of the original sentence starting from index 7 to the end
print(sentence[7:])  # Output: great!

# Print the original sentence in reverse order, effectively inverting it
print(sentence[::-1])  # Output: !taerg era uoY
are g
aeg
 great!
!taerg era uoY

TIPS: String Slicing

string[start:stop:step]

Note that single space is counted as one character:

print(len(sentence))
14

Concatenation is combining two or more strings together using the + operator.

# String concatenation
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)  # Output: John Doe
John Doe

2 String Methods

“Python” language provides several built-in methods (similar to functions) for manipulating strings:

  • upper() converts a string to uppercase
  • lower() converts a string to lowercase
  • strip() removes leading and trailing whitespaces
  • replace() replaces a substring with another substring
  • split() splits a string into a list based on a delimiter
  • join() joins a list of strings into a single string using a specified delimiter.
# Using string methods
name = "John Doe"
print(name.upper())  # Output: JOHN DOE
print(name.lower())  # Output: john doe

message = "   Hello, World!   "
print(message.strip())  # Output: Hello, World!

quote = "Life is short, take this stone"
print(quote.replace("stone", "novel"))  # Output: Life is short, take this novel

numbers = "1,2,3,4,5"
numbers_list = numbers.split(",")
print(numbers_list)  # Output: ['1', '2', '3', '4', '5']

fruits = ['apple', 'banana', 'cherry']
fruits_string = ', '.join(fruits)  # ", " is the delimiter
print(fruits_string)  # Output: apple, banana, cherry
JOHN DOE
john doe
Hello, World!
Life is short, take this novel
['1', '2', '3', '4', '5']
apple, banana, cherry

In conclusion, understanding how to handle strings in “Python” programming is fundamental for any programmer. Strings play a crucial role in storing and manipulating text data, and “Python” language offers a wide range of tools and methods to work with them effectively.

3 Exercises