DEV Community

Keerti Sadana S
Keerti Sadana S

Posted on Edited on

PYTHON - STRING METHOD EXPLANATIONS

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

name = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print(name[0:26])
print()
print(name[0:26:2])
print()
print(name[0:26:3])
print()
print(name[:])
print()
print(name[::2])
print()
print(name[::3])
print()
print(name[5:10])
print()
print(name[:10])
print()
print(name[20:])
print()
print(name[5::2])
print()
print(name[-1::-1])
print()
print(name[::-1])
print()
Enter fullscreen mode Exit fullscreen mode

Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

ACEGIKMOQSUWY

ADGJMPSVY

ABCDEFGHIJKLMNOPQRSTUVWXYZ

ACEGIKMOQSUWY

ADGJMPSVY

FGHIJ

ABCDEFGHIJ

UVWXYZ

FHJLNPRTVXZ

ZYXWVUTSRQPONMLKJIHGFEDCBA

ZYXWVUTSRQPONMLKJIHGFEDCBA

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() - used to convert a string to lowercase. It is similar to the Python lower() string method, but it removes all the case distinctions present in a string.
-> 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).
-> isdigit() - Returns True if all characters are basic digits (0-9) and certain Unicode subscripts/superscripts.
-> isnumeric() - Returns True if all characters are numeric, which includes digits, fractions, Roman numerals, and foreign currency numerals.
-> format() - used to inject dynamic values into template strings.
Refer to the examples for the above methods:
https://dev.to/keerti_sadanas_d3bbb8924/python-string-methods-examples-4im

Top comments (0)