[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

String Functions

The document provides a comprehensive overview of various string functions in Python, detailing their methods, descriptions, and syntax. Key functions include len(), title(), lower(), upper(), isalpha(), and more, each serving specific purposes for string manipulation. Additionally, it covers methods for string partitioning, splitting, stripping, replacing, and joining elements.
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)
4 views3 pages

String Functions

The document provides a comprehensive overview of various string functions in Python, detailing their methods, descriptions, and syntax. Key functions include len(), title(), lower(), upper(), isalpha(), and more, each serving specific purposes for string manipulation. Additionally, it covers methods for string partitioning, splitting, stripping, replacing, and joining elements.
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
You are on page 1/ 3

String Functions

Method Description Syntax


len() Returns the length of the given string len(string)
title() Returns the string with first letter of every word in the string in uppercase and rest in lowercase str.title()
lower() Returns the string with all uppercase letters converted to lowercase str.lower()
upper() Returns the string with all lowercase letters converted to uppercase str.upper()
isalpha() Returns True if characters of the given string are alphabets (uppercase or lowercase). If whitespace or str.isalpha()
special symbols are part of the given string or the string is empty it returns False.

isdigit() Returns True if characters of the given string are numeric. If whitespace or special symbols are part of str.isdigit()
the given string or the string is empty it returns False.

isalnum() Returns True if characters of the given string are either alphabets or numeric. If whitespace or special str.isalnum()
symbols are part of the given string or the string is empty it returns False.

islower() Returns True if the string is non-empty and has all lowercase alphabets, or has at least one character str.islower()
as lowercase alphabet and rest are non-alphabet characters.

isupper() Returns True if the string is non-empty and has all uppercase alphabets, or has at least one character str.isupper()
as uppercase character and rest are non-alphabet characters.

isspace() Returns True if the string is non-empty and all characters are white spaces (blank, tab, newline, str.isspace()
carriage return).

istitle() Returns True if the string is non-empty and title case, i.e., the first letter of every word in the string in str.istitle()
uppercase and rest in lowercase.

title() returns a copy of the original string and converts the first character each word of the string to a str.title()
capital (uppercase) letter while making all other characters in the string lowercase letters.

capitalize() returns a copy of the original string and converts the first character of the string to a capital str.capitalize()
(uppercase) letter while making all other characters in the string lowercase letters.
String Functions
Method Description Syntax
startswith() ▪ Al least one argument is required. str.startswith(substr[,start,end])
▪ Returns True if the given string starts with the supplied substring otherwise returns False.
endswith() ▪ Al least one argument is required. str.endswith(substr[,start,end])
▪ Returns True if the given string ends with the supplied substring otherwise returns False.
count() ▪ Returns the number of times the substring substr present in the given string. str.count(substr[,start,end])
▪ If we do not give start index and end index then searching starts from index 0 and ends at
length of the string
If the substring is not present in the given string, then the function returns 0.
find() ▪ Returns the first occurrence of index of substring str occurring in the given string. str.find(substr[,start,end])
▪ If we do not give start and end then searching starts from index 0 and ends at length of the
string.
If the substring is not present in the given string, then the function returns -1.
index() Same as find() function but raises an error/exception (ValueError) if the substring is not present in the str.index(substr[,start,end])
given string.

partition() ▪ It takes exactly one argument. str.partition(separator)


▪ Partitions the given string at the first occurrence of the substring (separator) and returns the
string partitioned into three parts.
1. Substring before the separator
2. Separator
3. Substring after the separator
If the separator is not found in the string, it returns the whole string itself and two empty strings.
▪ The result is stored into a tuple.
split() ▪ It takes two optional arguments. str.split([separator, maxsplit])
Returns a list of words delimited by the specified substring. If no delimiter is given, then words are
separated by space.
String Functions
Method Description Syntax
strip() ▪ It takes exactly one optional argument. str.strip([character(s)])
▪ Returns a copy of the string with leading and trailing characters removed (based on the string
argument passed).
▪ If argument is passed, it removes the leading and trailing spaces.

lstrip() ▪ It takes exactly one optional argument. str.lstrip([character(s)])


▪ returns a copy of the string with leading characters removed (based on the string argument
passed).
▪ If no argument is passed, it removes the leading spaces.

rstrip() ▪ It takes exactly one optional argument. str.rstrip([character(s)])


▪ Returns a copy of the string with trailing characters removed (based on the string argument
passed).
▪ If no argument is passed, it removes the trailing spaces.

replace() ▪ It takes 3 arguments with the 3rd argument as an optional argument. str.replace(old, new[, count])
▪ returns a copy of the string where all occurrences of a substring are replaced with another
substring.

join() ▪ It takes exactly one argument. string_name.join(iterable)


▪ returns a string in which the elements of sequence have been joined by str separator

You might also like