DEV Community

CNavya21
CNavya21

Posted on

PYTHON : STRING METHODS

capitalize()

  • It returns the string in the form of first character is upper case and the rest in lower case.

Syntax

string.capitalize()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "hi, welcome!"
a = txt.capitalize()
print(a)
#output:Hi, welcome!
Enter fullscreen mode Exit fullscreen mode

casefold()

  • It returns the string where all the characters are lower case.

Syntax

string.casefold()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "This Is My Example"
x = txt.casefold()
print(x)
#output: this is my example
Enter fullscreen mode Exit fullscreen mode

center()

  • It is a string class function that is used to position a string by padding it with a specified character.

Syntax

string.center(length, character)
#length - length of the required string.
#character - to fill the missing space on each side and default is " "(space).
Enter fullscreen mode Exit fullscreen mode

Example

txt = "banana"
x = txt.center(20, "x")
print(x)
#output: xxxxxxxbananaxxxxxxx
Enter fullscreen mode Exit fullscreen mode

count()

  • This method returns the number of times a specified value appears in the string.

Syntax

string.count(value, start, end)
#value - value to search for a string
#start - position to start the search and default is 0
#end - position to end the search and default is end of the string
Enter fullscreen mode Exit fullscreen mode

Example

txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)
#output: 1
Enter fullscreen mode Exit fullscreen mode

endswith()

  • This method method returns True if the string ends with the specified value, otherwise False.

Syntax

string.endswith(value, start, end)
Enter fullscreen mode Exit fullscreen mode

Example

txt = "Hello, welcome to my world."
x = txt.endswith("!")
print(x)
#output:False
Enter fullscreen mode Exit fullscreen mode

expandtabs()

  • This method sets the tab size to the specified number of whitespaces.

Syntax

string.expandtabs(tabsize)
#tabsize - a number specifying the tabsize and default is 8.
Enter fullscreen mode Exit fullscreen mode

Example

txt = "W\te\tl\tc\to\tm\te"
print(txt.expandtabs(1)) 
#output: W e l c o m e
Enter fullscreen mode Exit fullscreen mode

find()

  • It finds the first occurrence of the specified value.
  • It will returns -1 if the value not found.
  • This method is almost same as index() method, the only difference is that the index() method rasie an exception if the value is not found.

Syntax

string.find(value, start, end)
Enter fullscreen mode Exit fullscreen mode

Example

txt = "Hello, welcome to my world."
x = txt.find("n")
print(x)
#output: -1
Enter fullscreen mode Exit fullscreen mode

format()

  • This method formats the specified value or values and insert them inside the string placeholder.
  • the placeholder is defined using curly brackets {}.

Syntax

string.format(value1, value2,....)
# values can required one or more values . 
Enter fullscreen mode Exit fullscreen mode

Example

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))
#output: For only 49.00 dollars! 
Enter fullscreen mode Exit fullscreen mode

index()

  • This method finds the first occurrence of the specified value.
  • It raises an exception if the value is not found.

Syntax

string.index(value, start, end)
Enter fullscreen mode Exit fullscreen mode

Example

txt = "Hello, welcome to my world."
x = txt.index("m")
print(x)
#ouput: 12
Enter fullscreen mode Exit fullscreen mode

isalnum()

  • This method returns true if all the characters are alphanumeric ,means letter(a-z) and numbers (0-9).

Syntax

string.isalnum()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "company123"
x = txt.isalnum()
print(x)
#output: True
Enter fullscreen mode Exit fullscreen mode

isalpha()

  • It returns true if all the characters are alphabet letters(a-z).

Syntax

string.isalpha()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "company123"
x = txt.isalpha()
print(x)
#output: False
Enter fullscreen mode Exit fullscreen mode

isascii()

  • It returns True if all the characters are ascii characters(a-z).

Syntax

string.isascii()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "company123"
x = txt.isascii()
print(x)
#output: true
Enter fullscreen mode Exit fullscreen mode

isdecimal()

  • It returns True if all the characters are decimanls(0-9).

Syntax

string.isdecimal()
Enter fullscreen mode Exit fullscreen mode

Example

a = "\u0030"
print(a.isdecimal())
#ouput: True
Enter fullscreen mode Exit fullscreen mode

swapcase()

  • It returns a string where all the upper case letters to lower case and vise versa.

Syntax

string.swapcase()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "Hello My Name Is PETER"
x = txt.swapcase()
print(x) 
#output:hELLO mY nAME iS peter 
Enter fullscreen mode Exit fullscreen mode

join()

  • It takes all items in an iterable and joins them into one string.

Syntax

string.join(iterable)
Enter fullscreen mode Exit fullscreen mode

Example

myDict = {"name": "John", "country": "Norway"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)
#ouput: nameTESTcountry 
Enter fullscreen mode Exit fullscreen mode

split()

  • It splits into a list.

Syntax

string.split(separator, maxsplit)
#separator - use when splitting the string and by default any whitespace is a separator
# maxsplit - how many splits to do and default value is -1, which is "all occurrences"
Enter fullscreen mode Exit fullscreen mode

Example

txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
#output: ['apple', 'banana', 'cherry', 'orange']
Enter fullscreen mode Exit fullscreen mode

zfill()

  • It adds zeros at the beginning of the string, until it reaches the specified length.

Syntax

string.zfill(len)
Enter fullscreen mode Exit fullscreen mode

Example

txt = "50"
x = txt.zfill(10)
print(x)
#output: 0000000050
Enter fullscreen mode Exit fullscreen mode

upper()

  • It returns a string where all characters are in upper case.

Syntax

string.upper()
Enter fullscreen mode Exit fullscreen mode

Example

txt = "hello"
a = txt.upper()
print(a)
# output: HELLO
Enter fullscreen mode Exit fullscreen mode

Top comments (0)