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

Python String Methods Overview

The document provides a comprehensive list of string methods in Python, detailing their functionality and usage. Each method is described with its purpose, such as capitalization, case handling, and string manipulation. It also includes information on deprecated methods and the expected return values for each function.

Uploaded by

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

Python String Methods Overview

The document provides a comprehensive list of string methods in Python, detailing their functionality and usage. Each method is described with its purpose, such as capitalization, case handling, and string manipulation. It also includes information on deprecated methods and the expected return values for each function.

Uploaded by

sahiljamwal2720
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Method Description

capitalize() It capitalizes the first character of the String. This function is deprecated in python3

casefold() It returns a version of s suitable for case-less comparisons.

center(width ,fillchar) It returns a space padded string with the original string centred with equal number of left and right
spaces.

count(string,begin,end) It counts the number of occurrences of a substring in a String between begin and end index.

decode(encoding = 'UTF8', errors = Decodes the string using codec registered for encoding.
'strict')

encode() Encode S using the codec registered for encoding. Default encoding is 'utf-8'.

endswith(suffix It returns a Boolean value if the string terminates with given suffix between begin and end.
,begin=0,end=len(string))

expandtabs(tabsize = 8) It defines tabs in string to multiple spaces. The default space value is 8.

find(substring ,beginIndex, endIndex) It returns the index value of the string where substring is found between begin index and end index.

format(value) It returns a formatted version of S, using the passed value.

index(subsring, beginIndex, endIndex) It throws an exception if string is not found. It works same as find() method.

isalnum() It returns true if the characters in the string are alphanumeric i.e., alphabets or numbers and
there is at least 1 character. Otherwise, it returns false.

isalpha() It returns true if all the characters are alphabets and there is at least one character, otherwise False.

isdecimal() It returns true if all the characters of the string are decimals.

isdigit() It returns true if all the characters are digits and there is at least one character, otherwise False.

isidentifier() It returns true if the string is the valid identifier.

islower() It returns true if the characters of a string are in lower case, otherwise false.

isnumeric() It returns true if the string contains only numeric characters.

isprintable() It returns true if all the characters of s are printable or s is empty, false otherwise.

isupper() It returns false if characters of a string are in Upper case, otherwise False.

isspace() It returns true if the characters of a string are white-space, otherwise false.

istitle() It returns true if the string is titled properly and false otherwise. A title string is the one in which the first c
is upper-case whereas the other characters are lower-case.

isupper() It returns true if all the characters of the string(if exists) is true otherwise it returns false.

join(seq) It merges the strings representation of the given sequence.

len(string) It returns the length of a string.


ljust(width[,fillchar]) It returns the space padded strings with the original string left justified to the given width.

lower() It converts all the characters of a string to Lower case.

lstrip() It removes all leading whitespaces of a string and can also be used to remove particular character from lea

partition() It searches for the separator sep in S, and returns the part before it, the separator itself, and the part after
separator is not found, return S and two empty strings.

maketrans() It returns a translation table to be used in translate function.

replace(old,new[,count]) It replaces the old sequence of characters with the new sequence. The max characters are replaced if max

rfind(str,beg=0,end=len(str)) It is similar to find but it traverses the string in backward direction.

rindex(str,beg=0,end=len(str)) It is same as index but it traverses the string in backward direction.

rjust(width,[,fillchar]) Returns a space padded string having original string right justified to the number of characters specified.

rstrip() It removes all trailing whitespace of a string and can also be used to remove particular character from trail

rsplit(sep=None, maxsplit = -1) It is same as split() but it processes the string from the backward direction. It returns the list of words in th
If Separator is not specified then the string splits according to the white-space.

split(str,num=[Link](str)) Splits the string according to the delimiter str. The string splits according to the space if the delimiter is not p
It returns the list of substring concatenated with the delimiter.

splitlines(num=[Link]('\n')) It returns the list of strings at each line with newline removed.

startswith(str,beg=0,end=len(str)) It returns a Boolean value if the string starts with given str between begin and end.

strip([chars]) It is used to perform lstrip() and rstrip() on the string.

swapcase() It inverts case of all characters in a string.

title() It is used to convert the string into the title-case i.e., The string meEruT will be converted to Meerut.

translate(table,deletechars = '') It translates the string according to the translation table passed in the function .

upper() It converts all the characters of a string to Upper Case.

Common questions

Powered by AI

'swapcase()' inverts the case of each character in a string, transforming uppercase letters to lowercase and vice versa. In contrast, 'upper()' converts all letters in the string to uppercase and 'lower()' to lowercase. 'swapcase()' is useful for toggling text case in a format-exploratory manner, whereas 'upper()' and 'lower()' are used for normalization to a specific case .

The 'find()' method searches for a substring from the beginning of a string and returns the index of the first occurrence, whereas 'rfind()' searches from the end of the string towards the beginning, returning the last occurrence. This distinction makes 'rfind()' useful for parsing the last sections of strings or checking for suffixes .

'strip()' is used to remove leading and trailing whitespace or specified characters from a string, 'lstrip()' removes them only from the beginning, and 'rstrip()' only from the end. These methods are essential in data cleaning where extra spaces or delimiters need to be trimmed from data inputs to ensure uniformity and accuracy before processing or analysis .

Using 'casefold()' is more appropriate when performing case-insensitive comparisons in locale-sensitive environments, such as when processing text that might contain international characters. 'casefold()' is designed to be more aggressive in standardizing text by considering more case variations (such as those in other languages) than 'lower()', which simply converts uppercase ASCII letters to lowercase .

In a file management system, 'endswith()' can be used to filter and process specific file types. For instance, checking if a file name ends with '.txt' when you want to gather all text files in a directory for reading. This method quickly distinguishes among multiple file formats and automates workflows involving specific file types .

In user input validation, 'startswith()' and 'endswith()' can be used to enforce conditions regarding prefixes or suffixes. For example, ensuring email addresses start with valid alphanumeric characters or that URLs end with recognized domain extensions like '.com'. These logical checks ensure compliance with expected formats for file paths, email validations, or API endpoint verifications .

'join()' is preferred over the concatenation operator for its efficiency in handling large sequences of strings because it minimizes the memory overhead. It does so by internally calculating the necessary memory allocation once and creating the new string in a single operation. In contrast, repeated concatenation could lead to many inefficient memory reallocations and poor performance .

The 'capitalize()' method only capitalizes the first character of the string and lowers the rest, while the 'title()' method capitalizes the first letter of each word in the string. This makes 'title()' suitable for formatting names and titles, whereas 'capitalize()' is more for sentences where only the initial letter needs capitalizing .

'isdecimal()' checks if all characters in a string are decimal characters, which includes only digit characters used in numbers. 'isdigit()' checks for digits and may include characters like superscripts and Roman numerals in some locales. Thus, 'isdecimal()' is suitable for strict validation of numeric values (e.g., performing arithmetic operations), while 'isdigit()' can be used in more general contexts where numeric-like characters need to be recognized .

The 'count' parameter in 'replace()' specifies the maximum number of occurrences to replace in the string, offering granular control over the modification process. This is particularly useful when only a specific number of substitutions are desired or when adjusting certain portions without affecting other occurrences, thus maintaining partial original data integrity .

You might also like