Introduction
In this chapter, we will learn how to use Python String.
Python String
- String is a sequence of characters and each character can be individually accessed using an Index.
- Strings are stored as individual characters in a contiguous location, with a 2-way index for each location.
- We can create them simply by enclosing the characters in quotes (eg:- ‘Hello’).
Note
Python treats single quotes the same as double-quotes.
You cannot change the individual letters of a string in place by assignment because strings are immutable and hence item assignment is not supported.
Traversing a string
(Iteration through elements of a string, one character at a time)
As mentioned above, individual characters of a string are accessible through a unique index of each character. Using these indexes, you can traverse a string character-by-character.
Using range()
STRING OPERATORS
String Concatenation
The + operator creates a new string by joining two operand strings.
Some Examples
NOTE
+ operator can work with Numbers and strings separately for Addition and Concatenation respectively; but in the same expression, you cannot combine numbers and strings as operand with + operator.
String Replication
- The * operator when used with numbers, performs Multiplication and returns the product of two numbers.
- To use an * operator in Strings, one operand must be a String and one must be a number.
Syntax
number*string or string*number
String operand tells the string to be replicated and number operand tells the number of times it is to be repeated.
Some Examples
NOTE
You cannot have strings as both the operands with * operator.
IN and NOT IN
There are two membership operators IN and NOT IN.
- IN- returns True if a character or a substring exists in the given string; False otherwise.
- NOT IN- returns True if a character or a substring does not exist in a given string; False otherwise.
Comparison Operators
- The comparison or relational operators in Python are: >, >=, <, <=, !=
- The comparison using these operators is based on the standard character-by-character comparison rule for Unicode.
- For most common characters ASCII values and Unicode values are the same.
Some Examples
STRING METHODS
string.isalnum()
This method returns True if the characters in the string are alphanumeric and there is at least one character; otherwise False.
string.isalpha()
This method returns True if the characters in the string are alphabetic and there must be at least one character; otherwise False.
string.isdigit()
This method returns True if the characters in the string are digits and there must be at least one character; otherwise False.
string.islower()
This method returns True if all cased characters in the string are lowercase and there must be at least one cased character; otherwise False.
string.isupper()
This method returns True if all cased characters in the string are uppercase and there must be at least one cased character; otherwise False.
string.isspace()
This method returns True if there are only whitespace characters in the string and there must be at least one character; otherwise False.
string.capitalize()
This method returns a copy of the string with its first character capitalized.
string.find(sub[,start[,end]])
- This method returns the lowest index in the string where the substring is found within the slice range of start and end.
- Returns -1 if a sub is not found.
string.upper()
This method returns a copy of the string converted to uppercase.
string.lower()
This method returns a copy of the string converted to lowercase.
Conclusion
In the next chapter, we will discuss Python Tuples.