Learn Python
Lesson 1 - Introduction To Python
Lesson 2 - Basic Python Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 5 - Basic Data Structures
Lesson 6 - Exception Handling
Lesson 7 - Modules And Packages
Lesson 9 - Object-Oriented Programming (OOP)
You've already learned how to declare a string variable.
In this lesson, we'll introduce a few basic string operations.
Concatenation: Concatenation allows you to combine two strings.
str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # Output: Hello World
The above program concatenates str1
and str2
.
Length: Find the length of a string as follows:
my_str = "Python" length = len(my_str) print(length) # Output: 6
Upper and Lower:
Python allows you to convert a string into uppercase and lowercase easily using the methods upper()
and lower()
. Here's an example:
my_string = "Hello World" upper_case = my_string.upper() lower_case = my_string.lower() print(upper_case) print(lower_case)
Split:
Python's split()
method enables you to split a string based on a specific separator.
my_string = "apple,banana,orange" fruits = my_string.split(",") print(fruits) # Output: ['apple', 'banana', 'orange']
Here, we used ,
as a separator. Note that the split()
method returns a list after splitting the string.