String Data Structure in Python:
A string is a sequence of characters and is one of the most basic data structures in programming. In Python, strings are represented using quotes, either single quotes ('
) or double quotes ("
). Strings are immutable, which means that once you create a string, you can't change its contents. However, you can create new strings from existing strings by concatenating or repeating them.
- Concatenation: You can use the
+
operator to concatenate two or more strings and create a new string. For example:
python>>> first_name = "John"
>>> last_name = "Doe"
>>> full_name = first_name + " " + last_name
>>> print(full_name)
John Doe
- Repetition: You can use the
*
operator to repeat a string a specified number of times. For example:
python>>> word = "Hello"
>>> print(word * 3)
HelloHelloHello
- Slicing: You can extract a portion of a string by slicing it. The slice consists of all the characters from a start index to an end index, excluding the character at the end index. For example:
python>>> word = "Hello"
>>> print(word[1:4])
ell
- Length: You can use the
len
function to find the number of characters in a string. For - example:
- python
>>> word = "Hello" >>> print(len(word)) 5
- Membership: You can use the
in
operator to test if a character or a substring is present in a string. For example:
python>>> word = "Hello" >>> print('H' in word) True >>> print('h' in word) False
- Comparison: You can compare two strings to see if they're equal, less than, or greater than each other. For example:
python>>> word1 = "Hello" >>> word2 = "hello" >>> print(word1 == word2) False >>> print(word1 < word2) False >>> print(word1 > word2) True
- Membership: You can use the
- Formatting: You can use the
format
method to insert values into a string. For example: - python
name = "John" message = "Hello, {}".format(name) print(message) Hello, Joh
a number of built-in methods for string manipulation and processing. Here are some of the most commonly used ones:
upper
: Converts all the characters in a string to uppercase. For example:
python>>> word = "Hello"
>>> print(word.upper())
HELLO
lower
: Converts all the characters in a string to lowercase. For example:
python>>> word = "Hello"
>>> print(word.lower())
hello
strip
: Removes whitespaces from the beginning and end of a string. For example:
python>>> word = " Hello "
>>> print(word.strip())
Hello
replace
: Replaces all occurrences of a substring with another string. For example:
python>>> word = "Hello, World!"
>>> print(word.replace("World", "Python"))
Hello, Python!
split
: Splits a string into a list of substrings based on a specified separator. For example:
python>>> word = "Hello, World!"
>>> print(word.split(","))
['Hello', ' World!']
join
: Joins a list of strings into a single string, using a specified separator. For example:
python>>> words = ["Hello", "World"]
>>> print(", ".join(words))
Hello, World
These are just a few of the many string operations and methods available in Python. By combining these operations and methods, you can perform a wide range of string processing tasks in an elegant and efficient manner.
Comments
Post a Comment