DEV Community

Keerti Sadana S
Keerti Sadana S

Posted on

PYTHON - STRING

About Strings:
-> Strings are sequences of characters written inside quotes.
-> They can include letters, numbers, symbols and spaces.
-> Python does not have a separate character type.
-> Strings are indexed sequences.
-> Positive indices start at 0 from the left; negative indices start at -1 from the right, as represented in the image:

-> 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.

What is Immutability?
Existing memory won't be affected. Instead, the change will be updated in new memory.

HashCode:
HashCode is the hexadecimal value of the address.
It is called as hash().

Garbage Collection:
-> An unused object, memory, or function is known as garbage.
-> The process is called garbage collection
-> To use garbage collection functions, import the gc module

Slicing:
-> Slicing is a way to extract a portion of a string by specifying the start and end indices.
Syntax:
str_name[start, stop, step]

Explanation:
-> start - Starting number
-> stop - Ending number + 1
-> step - Increment / Decrement

String Functions:

-> upper() - makes the full string uppercase
-> lower() - makes the full string lowercase
-> capitalize() - only the first letter becomes uppercase
-> title() - capitalises the first letter of every word.
-> split() - breaks a string into a list of smaller strings based on a separator
-> join() - merges a list or iterable of strings into one single string, using the host string as the connector.
-> startswith() - returns True if the string begins with the specified value;
-> endswith() - returns True if the string ends with the specified value.
-> casefold() - returns True if the string begins with the specified value.
-> swapcase() - swaps uppercase letters to lowercase, and lowercase letters to uppercase.
-> strip() - removes all leading and trailing whitespace
-> lstrip() - removes whitespace (or specific characters) only from the left (start) of the string.
-> rstrip() - removes whitespace (or specific characters) only from the right (end) of the string.
-> isspace() - returns True if all characters in the string are whitespace.
-> isalnum() - returns True if all characters in the string are alphanumeric (letters or numbers).
-> isnumeric() - Returns True if all characters are basic digits (0-9) and certain Unicode subscripts/superscripts.
-> isdigit() - Returns True if all characters are numeric, which includes digits, fractions, Roman numerals, and foreign currency numerals.

Top comments (0)