Python Python Tutorial File Handling NumPy Tutorial NumPy Random NumPy ufunc Pandas Tutorial Pandas Cleaning Data Pandas Correlations Pandas Plotting SciPy Tutorial



Python Strings

Python is a high-level programming language that is widely used for web development, data analysis, artificial intelligence, and scientific computing. One of the most important data types in Python is the string. In this article, we will discuss Python strings, their properties, and how to manipulate them.

What are Python Strings?

A string is a sequence of characters enclosed in quotation marks. In Python, strings can be enclosed in single quotes ('...') or double quotes ("..."). For example:


# Single quotes
string1 = 'Hello, World!'

# Double quotes
string2 = "Python is awesome!"

Strings can also be enclosed in triple quotes ('''...''') or ("""...""") to span multiple lines. For example:


string3 = '''This is a multi-line
string that spans two lines.'''

string4 = """This is also a multi-line
string that spans two lines."""

Properties of Python Strings

Strings in Python are immutable, which means that once a string is created, it cannot be changed. However, we can create a new string by concatenating two or more strings. For example:


string1 = 'Hello, '
string2 = 'World!'
string3 = string1 + string2

print(string3) # Output: Hello, World!

Strings in Python are also indexed, which means that we can access individual characters in a string using their index. The index of the first character in a string is 0, and the index of the last character is len(string) - 1. For example:


string = 'Python'

print(string[0]) # Output: P
print(string[1]) # Output: y
print(string[-1]) # Output: n

Strings in Python are also sliced, which means that we can extract a substring from a string using the slice operator [:]. The slice operator takes two indices, start and end, and returns the substring from start to end-1. For example:


string = 'Python'

print(string[0:2]) # Output: Py
print(string[2:4]) # Output: th
print(string[4:]) # Output: on

Manipulating Python Strings

Python provides several built-in functions and methods for manipulating strings. Here are some examples:

len()

The len() function returns the length of a string. For example:


string = 'Python'

print(len(string)) # Output: 6

lower()

The lower() method returns a new string with all the characters in lowercase. For example:


string = 'Python'

print(string.lower()) # Output: python

upper()

The upper() method returns a new string with all the characters in uppercase. For example:


string = 'Python'

print(string.upper()) # Output: PYTHON

strip()

The strip() method returns a new string with all the leading and trailing whitespace removed. For example:


string = '   Python   '

print(string.strip()) # Output: Python

replace()

The replace() method returns a new string with all the occurrences of a substring replaced with another substring. For example:


string = 'Python is awesome!'

print(string.replace('awesome', 'great')) # Output: Python is great!

split()

The split() method returns a list of substrings separated by a delimiter. For example:


string = 'Python is awesome!'

print(string.split()) # Output: ['Python', 'is', 'awesome!']

Conclusion

Python strings are an important data type in Python that are widely used for text processing and manipulation. In this article, we discussed the properties of Python strings, how to manipulate them, and some built-in functions and methods for working with strings.

References

Activity