Table of contents | |
Introduction | |
Creating Strings | |
Accessing Characters in a String | |
String Slicing | |
String Concatenation | |
String Length | |
String Methods | |
Sample Problems |
In Python, strings are an essential data type used to represent text. They are sequences of characters enclosed in either single quotes (' ') or double quotes (" "). In this article, we will explore the basics of strings in Python, including creating and manipulating strings, accessing individual characters, string concatenation, and various string methods.
To create a string in Python, you can simply assign a sequence of characters to a variable. Here are a few examples:
name = "Alice"
message = 'Hello, World!'
empty_string = ""
You can access individual characters within a string by using the index. In Python, indexing starts from 0, meaning the first character has an index of 0. Here's an example:
name = "Alice"
print(name[0]) # Output: A
print(name[2]) # Output: i
String slicing allows you to extract a portion of a string. It is done by specifying the starting and ending indices within square brackets. The ending index is exclusive. Let's see an example:
message = "Hello, World!"
print(message[0:5]) # Output: Hello
print(message[7:]) # Output: World!
String concatenation is the process of combining two or more strings. In Python, you can concatenate strings using the + operator. Here's an example:
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) # Output: John Doe
To determine the length of a string (i.e., the number of characters), you can use the len() function. Here's an example:
message = "Hello, World!"
print(len(message)) # Output: 13
Python provides a variety of built-in methods to manipulate strings. Here are a few commonly used methods:
Let's see these methods in action:
message = " Hello, World! "
print(message.lower()) # Output: hello, world!
print(message.strip()) # Output: Hello, World!
print(message.split(',')) # Output: [' Hello', ' World! ']
print(message.replace('o', 'a')) # Output: Hella, Warld!
Problems 1: Write a program that asks the user to enter their name and prints a welcome message with the name capitalized.
name = input("Enter your name: ")
print("Welcome, " + name.capitalize() + "!")
Problems 2: Write a program that counts the number of occurrences of a specific character in a given string.
def count_char(string, char):
count = 0
for c in string:
if c == char:
count += 1
return count
message = "Hello, World!"
print(count_char(message, 'l')) # Output: 3
In this article, we covered the basics of strings in Python. We learned how to create strings, access individual characters, perform string slicing, concatenate strings, determine the length of a string, and use some useful string methods. By understanding these concepts, you'll be well-equipped to work with strings and handle various string operations in Python.
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|