Python String functions
In Python 3, there are many built-in functions for working with strings. Here are some of the most commonly used string functions:
-
len(string)
: Returns the length of the string. -
str(object)
: Converts an object to a string representation. -
str.capitalize()
: Returns a copy of the string with its first character capitalized and the rest in lowercase. -
str.upper()
: Returns a copy of the string with all the characters in uppercase. -
str.lower()
: Returns a copy of the string with all the characters in lowercase. -
str.strip()
: Returns a copy of the string with leading and trailing whitespaces removed. -
str.replace(old, new)
: Returns a copy of the string with all occurrences ofold
replaced withnew
. -
str.split(sep)
: Returns a list of substrings separated bysep
. -
str.join(iterable)
: Returns a string that concatenates the elements ofiterable
using the string as a separator. -
str.format(*args, **kwargs)
: Formats the string using the positional and keyword arguments. -
str.isdigit()
: ReturnsTrue
if all characters in the string are digits andFalse
otherwise. -
str.isalpha()
: ReturnsTrue
if all characters in the string are alphabetic andFalse
otherwise. -
str.islower()
: ReturnsTrue
if all characters in the string are in lowercase andFalse
otherwise. -
str.isupper()
: ReturnsTrue
if all characters in the string are in uppercase andFalse
otherwise.
These are just a few of the most commonly used string functions in Python 3. For a complete list, you can refer to the official Python documentation.
Lenght Of String
len() function is used to find the lenght of string. It returns the integer number.
str_ = "python programming"
print(len(str_)) # return 18
Slicing The String
slicing string means , extract the substring from string as given index and return new string.
syntax of slicing string str[start:end]
end excluded.
str_ = "python programming"
print(str_[4:10]) # returns 'on pro'
print(str_[:]) # return 'python programming'
print(str_[:6]) # return 'python'
print(str_[-11:-4]) # returns 'program'
Count() Function In String
count()
function count the given string and return the numbers of occurances in string and return integer number. count() function may takes three arguments str.count("string",start,end)
str_ = "python Programming"
print(str_.count("o")) # returns 2
Strings Split() Function
split()
functions split the string at the occurance of given string and return the list.
str_ = "python Programming"
print(str_.split(" ")) # return ['python', 'Programming']
Strings Strip() Function
strip()
function is used to remove extra spaces from string. it will remove all white space such as beggining of string.
str_ = " python Programming is easy to learn."
print(str_.strip()) # return python Programming is easy to learn.
Strings Replace() Function
replace()
function is used to replace the string in a string. It will replace all occurances.
str_ = "python Programming is easy to learn."
print(str_.replace("r","R")) # return python PRogRamming is easy to leaRn.
String Upper() And Lower() Function
upper()
function turn all the character uppercase and lower()
function turn the all characters lowercase and return.
str_ = "python Programming"
print(str_.upper()) # returns PYTHON PROGRAMMING
print(str_.lower()) # returns python programming
String Capitalize() Function
capitalize()
function change the first letter uppercase in string.
str_ = "hello world"
print(str_.capitalize()) # Hello world
String Casefold() Function
casefold()
function change the letter to lowercase same as lower()
function.
str_ = "Hello World "
print(str_.casefold()) # hello world
String Startswith() Function
startswith()
function return True if the string is starting with specified value else return False.
str_ = "hello python\n programmer"
print(str_.startswith("python")) # False
print(str_.startswith("hel")) # True
String Endswith() Function
endswith()
if string is endswith specific value it will return True else False. endswith() function may takes three arguments endswith("string",start,ends)
str_ = "Hello World "
print(str_.endswith("ld")) # False
str_ = "Hello World"
print(str_.endswith("ld")) # True
print(str_.endswith("ld",6)) # True
String Find() Function
find()
return the index of first occurance of value else return the -1 . it may take three arguments str.find("str",start, end)
str_ = "Hello World "
print(str_.find("s")) # -1
print(str_.find("o")) # 4
String Format() Function
format()
format the string with placeholder . we can specify the placeholder within this bracket {}
here is the Example:
str_ = "Hello World {python} "
print(str_.format(python="with python")) # output - Hello World with python
String Index() Function
index()
function return the index of first occurance value, if specified value was found in string else raise exception as "substring not found". this function may take upto three arguments str.index("str",start,end)
str_ = "Hello World"
print(str_.index("W")) # output - 6
String Isalnum() Function
isalnum()
function return True is the all characters in a string are a-z or 0-9 or A-Z .
str_ = "hello world 5 time"
print(str_.isalnum()) # output - False
str_ = "helloWorld5time"
print(str_.isalnum()) # output - True
String Isalpha() Function
isalpha()
function return True if all characters are alphabet in a string else false.
str_ = "hello time"
print(str_.isalpha()) # output - False
str_ = "helloWorld5time"
print(str_.isalpha()) # output - False
str_ = "helloWorldtime"
print(str_.isalpha()) # output - True
String Isascii() Function
isascii()
function return True if all characters are belongs to ascii character sets.
str_ = "hello time"
print(str_.isascii()) # output - True
str_ = "helloWorl534$^$#5time"
print(str_.isascii()) # output - True
str_ = "{ helloWorldtime"
print(str_.isascii()) # output - True
String Isdecimal() Function
isdecimal()
function return the True if all characters in a string are 0-9 else return False.
str_ = "3539 5"
print(str_.isdecimal()) # False
str_ = "8380033339"
print(str_.isdecimal()) # True
String Isdigit() Function
isdigit()
function return True if all characters in a string are digits else return False.
str_ = "3539 5"
print(str_.isdigit()) # False
str_ = "8380033339"
print(str_.isdigit()) # True
String Checking As Identifiers Using Isidentifier()
isidentifier()
function return True if string is valid identifiers else return False.
str_ = "HelloWorld"
print(str_.isidentifier()) # True
str_ = "Hello World"
print(str_.isidentifier()) # False
str_ = "_Hello5World"
print(str_.isidentifier()) # True
String Islower() Function
islower()
function return True if all characters is a string are lowercase else return False.
str_ = "HelloWorld"
print(str_.islower()) # False
str_ = "hello world"
print(str_.islower()) # True
str_ = "_hello5 world"
print(str_.islower()) # True
String Isnumeric() Function
isnumeric()
function return True if all characters is a string are 0-9 else return Fasle.
str_ = "398579387"
print(str_.isnumeric()) # True
str_ = "hello 835"
print(str_.isnumeric()) # Fasle
str_ = "-9837"
print(str_.isnumeric()) # Fasle
str_ = "98.37"
print(str_.isnumeric()) # Fasle
String Isprintable() Function
isprintable()
function return True if all characters are printable in a string else return False.
str_ = "3985\n79"
print(str_.isprintable()) # Fasle
str_ = "hello\t835"
print(str_.isprintable()) # Fasle
str_ = "Hello World"
print(str_.isprintable()) # True
str_ = "98.37"
print(str_.isprintable()) # True
String Isupper() Function
isupper()
function return True if all characters are in uppercase letter else return False.
print(isupper("Hello World"))
String Swapcase() Function
swapcase()
function return the string after converting uppercase to the lowercase and lowercase to uppercase .
str_ = "Hello World"
print(str_.swapcase()) # hELLO wORLD
str_ = "hello"
print(str_.swapcase()) # HELLO
str_ = "Hello World"
print(str_.swapcase()) # hELLO wORLD
String Join() Function
join()
function return the string after joining all the given iterable object.
str_ = ""
lst = ["hi", "I", "am", "developer"]
print(str_.join(lst)) # hiIamdeveloper
String Rfind() Function
rfind()
function return the index of last occurance of given value . if not found in string return -1 .
str_ = "hello python programmer"
print(str_.rfind("o")) # 15
String Rindex() Function
rindex()
function return the index of last occurance of given value . if not found in raise error as "substring not found".
str_ = "hello python programmer"
print(str_.rindex("o")) # 15
String Rsplit() Function
rsplit()
function return a list after spliting by specified seperator with maximum . syntax is str.rsplit(seperator, maxsplit)
str_ = "hello python programmer"
print(str_.rsplit(" ")) # ['hello', 'python', 'programmer']
String Splitlines() Function
splitlines()
return the list after spliting by "\n" occurance in a string.
str_ = "hello python\n programmer"
print(str_.splitlines()) # ['hello python', ' programmer']
String Strip() Function
strip()
function return the string after removing extra white space from start or end of string.
str_ = " hello python programmer "
print(str_.strip()) # hello python programmer
String Zfill() Function
zfill()
function return the string after adding the zero at first upto given size.
str_ = "hello python programmer"
print(str_.zfill(40)) # 00000000000000000hello python programmer