Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Last Updated :
07 Jul, 2022
Some of the string methods are covered in the set 3 below
String Methods Part- 1
More methods are discussed in this article
1. len() :- This function returns the
length of the string.
2. count("string", beg, end) :- This function
counts the occurrence of mentioned
substring in whole string. This function takes 3 arguments, s
ubstring, beginning position( by default 0) and end position(by default string length).
Python
# Python code to demonstrate working of
# len() and count()
str = "geeksforgeeks is for geeks"
# Printing length of string using len()
print (" The length of string is : ", len(str));
# Printing occurrence of "geeks" in string
# Prints 2 as it only checks till 15th element
print (" Number of appearance of ""geeks"" is : ",end="")
print (str.count("geeks",0,15))
Output:
The length of string is : 26
Number of appearance of geeks is : 2
3. center() :- This function is used to
surround the string with a character repeated both sides of string multiple times. By default the character is a space. Takes 2 arguments,
length of string and the character.
4. ljust() :- This function returns the
original string shifted to left that has a
character at its right. It left adjusts the string. By default the character is space. It also takes two arguments,
length of string and the character.
5. rjust() :- This function returns the
original string shifted to right that has a
character at its left. It right adjusts the string. By default the character is space. It also takes two arguments,
length of string and the character.
Python
# Python code to demonstrate working of
# center(), ljust() and rjust()
str = "geeksforgeeks"
# Printing the string after centering with '-'
print ("The string after centering with '-' is : ",end="")
print ( str.center(20,'-'))
# Printing the string after ljust()
print ("The string after ljust is : ",end="")
print ( str.ljust(20,'-'))
# Printing the string after rjust()
print ("The string after rjust is : ",end="")
print ( str.rjust(20,'-'))
Output:
The string after centering with '-' is : ---geeksforgeeks----
The string after ljust is : geeksforgeeks-------
The string after rjust is : -------geeksforgeeks
6. isalpha() :- This function returns true when all the characters in the string are
alphabets else returns false.
7. isalnum() :- This function returns true when all the characters in the string are
combination of numbers and/or alphabets else returns false.
8. isspace() :- This function returns true when all the characters in the string are
spaces else returns false.
Python
# Python code to demonstrate working of
# isalpha(), isalnum(), isspace()
str = "geeksforgeeks"
str1 = "123"
# Checking if str has all alphabets
if (str.isalpha()):
print ("All characters are alphabets in str")
else : print ("All characters are not alphabets in str")
# Checking if str1 has all numbers
if (str1.isalnum()):
print ("All characters are numbers in str1")
else : print ("All characters are not numbers in str1")
# Checking if str1 has all spaces
if (str1.isspace()):
print ("All characters are spaces in str1")
else : print ("All characters are not spaces in str1")
Output:
All characters are alphabets in str
All characters are numbers in str1
All characters are not spaces in str1
9. join() :- This function is used to
join a sequence of strings mentioned in its arguments with the string.
Python
# Python code to demonstrate working of
# join()
str = "_"
str1 = ( "geeks", "for", "geeks" )
# using join() to join sequence str1 with str
print ("The string after joining is : ", end="")
print ( str.join(str1))
Output:
The string after joining is : geeks_for_geeks
Python String Methods - Part 1
Similar Reads
Python String - ljust(), rjust(), center() Strings are sequences of characters that we can manipulate in many ways. Sometimes we need to align a string to the left, right, or centre within a given space. In this article, we will explore the Difference between ljust(), just() and center().Table of Contentstr.ljust()Syntax of ljust()Example of
2 min read
String rjust() and ljust() in python() 1. String rjust() The string rjust() method returns a new string of given length after substituting a given character in left side of original string. Syntax: string.rjust(length, fillchar) Parameters: length: length of the modified string. If length is less than or equal to the length of the origin
2 min read
List Methods in Python | Set 1 (in, not in, len(), min(), max()...) List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3
2 min read
Output of Python Programs | Set 23 (String in loops) Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? Python3 my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i ⦠g e e k s f o r g e e k s
2 min read
Program to print a doormat pattern having a string written in the center in Python Given the number of rows R and the number of columns C, the task is to print a doormat pattern as shown in the examples below. The number of columns in the mat's design must be 3 times the number of rows in the design. The doormat should have GeeksforGeeks printed in the center. The width of the mat
2 min read
Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...) Python in its definition provides methods to perform inplace operations, i.e. doing assignments and computations in a single statement using an operator module. Example x += y is equivalent to x = operator.iadd(x, y) Inplace Operators in PythonBelow are some of the important Inplace operators in Pyt
3 min read