Python provides a rich set of operations to work with strings. Here are some common string operations along with examples:
1. Concatenation (+
)
Combines two or more strings into one.
2. Repetition (*
)
Repeats a string a specified number of times.
3. Accessing Characters (Indexing)
Accesses individual characters in a string using zero-based indexing.
4. Slicing
Extracts a substring from a string.
5. Length of a String (len()
)
Returns the length of the string.
6. Changing Case
Changes the case of the string.
7. Striping Whitespace
Removes leading and trailing whitespace.
8. Splitting and Joining
Splits a string into a list of substrings and joins a list of strings into one.
9. Finding Substrings
Finds the index of a substring and checks for the presence of a substring.
10. Replacing Substrings
Replaces all occurrences of a substring with another substring.
11. String Formatting
Formats strings using various methods.
Using %
Operator:
Using
format()
Method:These examples cover the fundamental string operations in Python, which allow you to manipulate and interact with strings efficiently.
Comments
Post a Comment