0% found this document useful (0 votes)
10 views2 pages

Attributes and Functions For STR

The document describes various string methods in Python like those for formatting, searching, modifying and checking properties of strings.

Uploaded by

Muhammad Saqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Attributes and Functions For STR

The document describes various string methods in Python like those for formatting, searching, modifying and checking properties of strings.

Uploaded by

Muhammad Saqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

__add__: Using the + operator.

__mul__: Using the * operator.


__eq__: Using the == operator.
__ge__: Using the >= operator.
__gt__: Using the > operator.
__le__: Using the <= operator.
__lt__: Using the < operator.
__ne__: Using the != operator.

__mod__: Allows string formatting using the % operator.


__len__: Returns the length (number of characters) of the string.
__class__: Refers to the class of the object (in this case, str).
__contains__: Checks if a substring is present in the string using the in
operator.
__hash__: Returns a hash value for the string.

__dir__: Returns a list of valid attributes for the object.


__doc__: Contains the documentation (docstring) for the object.

__getitem__: Allows indexing and slicing operations on the string.

__getattribute__: Retrieves the value of an attribute of an object. When you


do obj.x
__format__: Specifies the formatting behavior for the object when using the
format() function.

__delattr__: Allows deleting an attribute from an object.


__getnewargs__: Returns a tuple containing arguments that can be used to recreate
the string object.
__getstate__: Returns the state for pickling.
__init__: The constructor method that initializes a new object.
__init_subclass__: A class method called when a subclass is created.
__iter__: Returns an iterator object for the string, allowing iteration over its
characters.
__new__: Creates a new instance of the object.
__reduce__ and __reduce_ex__: Used for pickling and unpickling objects.
__repr__: Returns a string representation of the object, often used for debugging.
__rmod__: Used for string formatting when the string is on the right side of the %
operator.
__rmul__: Used for repetition when the string is on the right side of the *
operator.
__setattr__: Sets the value of an attribute of an object.
__subclasshook__: Used to customize class inheritance behavior.

capitalize: Returns a copy of the string with the first character


capitalized.
title: Returns a title-cased version(all words first letter) of the
string.
upper: Converts the whole string to uppercase.
lower: Converts the whole string to lowercase.
swapcase: Returns a copy in which uppercase characters converted to lowercase and
vice versa.

count: Counts the occurrences of a substring in the string.


find: Finds the first occurrence of a substring in the string. Return -1 if
substring not found.
index: Returns the index of the first occurrence of a substring in the string.
Return error if substring not found.
rfind: Finds the last occurrence of a substring in the string. Return -1 if
substring not found.
rindex: Returns the last index of a substring in the string. Return error if
substring not found.

endswith: Checks if the string ends with a specified suffix.


startswith: Checks if the string starts with a specified prefix.

split: Splits the string into a list of substrings.


.split(" ")
rsplit: Splits the string into a list of substrings, starting from the
right.
splitlines: Splits the string into a list of lines.

casefold: Returns a casefolded version of the string for case-insensitive


comparison.
center: Returns a centered version of the string within a specified width
and padding. .center(20, '..')
zfill: Pads the string with zeros to a specified width.
.zfill(40)
rjust: Returns a right-justified version of the string within a specified
width.
ljust: Returns a left-justified version of the string within a specified
width.

strip: Removes leading(ending) and trailing(starting) whitespace characters


from the string.
rstrip: Removes trailing whitespace characters from the string.
lstrip: Removes leading whitespace characters from the string.

format: Formats the string using placeholders and values.


f'my name is {var}'
format_map: Formats the string using a mapping of placeholders to values.
replace: Replaces occurrences of a substring with another substring.
.replace('str1' , 'str2')
join: Joins a sequence of strings with the string as a separator.

istitle: Checks if the string is in title case.


islower: Checks if all characters in the string are lowercase.
isupper: Checks if all characters in the string are uppercase.
isalnum: Checks if all characters in the string are alphanumeric.
isalpha: Checks if all characters in the string are alphabetic.
isascii: Checks if all characters in the string are ASCII characters.
isdecimal: Checks if all characters in the string are decimal digits.
isdigit: Checks if all characters in the string are digits.
isidentifier: Checks if the string is a valid Python identifier.
isnumeric: Checks if all characters in the string are numeric.
isprintable: Checks if all characters in the string are printable.
isspace: Checks if all characters in the string are whitespace.

encode: Encodes the string using a specified encoding.


expandtabs: Expands tab characters to spaces in the string.
maketrans: Creates a translation table for use with the translate() method.
partition: Splits the string into three parts based on a separator.
removeprefix: Removes a specified prefix from the start of the string.
removesuffix: Removes a specified suffix from the end of the string.
rpartition: Splits the string into three parts based on a separator, starting from
the right.
translate: Translates the string according to a translation table.

You might also like