Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 8 - Basic String Operations

8.1 Basic String Operations

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.