p

python tutorial - Python String | Python String Methods | Part 2 - len, count, center, ljust, rjust, isalpha, isalnum, isspace & join - learn python - python programming



 python lenth

Learn Python - Python tutorial - python lenth - Python examples - Python programs

1. len() :- This function returns the length of the string.

length of a string
lower cased, otherwise false.

2. count(“string”, beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning position( by default 0) and end position(by default string length).

 python str

Learn Python - Python tutorial - python str - Python examples - Python programs

python - Sample - python code :

# Python code to demonstrate working of 
# len() and count()
str = "wikitechy is a best e-learning website"
  
# Printing length of string using len()
print (" The length of string is : ", len(str));
 
# Printing occurrence of "wikitechy" in string
# Prints 2 as it only checks till 15th element
print (" Number of appearance of ""wikitechy"" is : ",end="")
print (str.count("wikitechy",0,11))
click below button to copy the code. By Python tutorial team

python tutorial - Output :

The length of string is :  38
 Number of appearance of wikitchy is : 1

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 - Sample - python code :

# Python code to demonstrate working of 
# center(), ljust() and rjust()
str = "wikitechy"
  
# Printing the string after centering with '-'
print ("The string after centering with '-' is : ",end="")
print ( str.center(15,'-'))
 
# Printing the string after ljust()
print ("The string after ljust is : ",end="")
print ( str.ljust(15,'-'))
 
# Printing the string after rjust()
print ("The string after rjust is : ",end="")
print ( str.rjust(15,'-'))
click below button to copy the code. By Python tutorial team

python tutorial - Output :

The string after centering with '-' is : ---wikitechy---
The string after ljust is : wikitechy------
The string after rjust is : ------wikitechy

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 - Sample - python code :

# Python code to demonstrate working of 
# isalpha(), isalnum(), isspace()
str = "wikitechy"
str1 = "123456"
  
# 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")
click below button to copy the code. By Python tutorial team

python tutorial - 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 - Sample - python code :

# Python code to demonstrate working of 
# join()
str = "_"
str1 = ( "wiki", "techy" )
 
# using join() to join sequence str1 with str
print ("The string after joining is : ", end="")
print ( str.join(str1))
click below button to copy the code. By Python tutorial team
 python string

Learn Python - Python tutorial - python string - Python examples - Python programs

python tutorial - Output :

The string after joining is : wiki_techy

Wikitechy tutorial site provides you all the learn python , free python online course , best online course to learn python , learn how to program in python python training free

Related Searches to Python String Methods | Part 2 - len, count, center, ljust, rjust, isalpha, isalnum, isspace & join