![]() |
VOOZH | about |
Strings are sequence of characters written inside quotes. It can include letters, numbers, symbols and spaces. Python does not have a separate character type.
Strings can be created using either single ('...') or double ("...") quotes. Both behave the same.
GFG GeeksForGeeks
Use triple quotes ('''...''' ) or ( """...""") for strings that span multiple lines. Newlines are preserved.
I am Learning Python String on GeeksforGeeks I'm a Geek
Strings are indexed sequences. Positive indices start at 0 from the left, negative indices start at -1 from the right as represented in below image:
Example 1: Access specific characters through positive indexing.
A E
Note: Accessing an index out of range will cause an IndexError. Only integers are allowed as indices and using a float or other types will result in a TypeError.
Example 2: Read characters from the end using negative indices.
D B
Slicing is a way to extract a portion of a string by specifying the start and end indexes. The syntax for slicing is string[start:end], where start starting index and end is stopping index (excluded).
BCD ABC DEF FEDCBA
Strings are iterable, which means we can access each character one by one using a loop.
A B C D E F
Explanation: for loop goes through the string in order and each iteration prints the next character.
Strings are immutable, meaning their values cannot be changed after creation. Any modification to a string creates a new string instead of altering the original one.
ABCDEF
Explanation: original string "aBCDEF" is not modified. A new string is created by combining "A" with s[1:] (rest of the string).
Individual characters of a string cannot be deleted because strings are immutable. However, an entire string variable can be removed using del keyword.
Note: After deleting a string, if we try to access it, Python raises a NameError because the variable no longer exists in memory.
Strings cannot be changed directly after creation. So any modification results in a new string being created using slicing or methods like replace().
HBCD EF abcD EF
Explanation:
Python provides various built-in methods to manipulate strings. Below are some of the most useful methods:
1. len(): returns the total number of characters in a string (including spaces and punctuation).
13
2. upper() and lower():upper() method converts all characters to uppercase whereas, lower() method converts all characters to lowercase.
HELLO WORLD hello world
3. strip() and replace():strip() removes leading and trailing whitespace from the string and replace() replaces all occurrences of a specified substring with another.
ABC Python is awesome
To learn more about string methods, please refer to Python String Methods.
1. Concatenation: Strings can be combined by using + operator.
Hello World
2. Repetition: A string can be repeated multiple times using *.
Hello Hello Hello
1. Using f-strings:f-strings allows to directly insert variables and expressions inside a string using {} brackets.
Name: Jake, Age: 22
2. Using format(): format() method allows inserting values into placeholders {} inside a string.
My name is Emily and I am 22 years old.
in keyword is used to check whether a substring exists inside a string. It returns True if the substring is found, otherwise it returns False.
True False