Skip to content

Latest commit

 

History

History
373 lines (309 loc) · 6.73 KB

StringMethods.md

File metadata and controls

373 lines (309 loc) · 6.73 KB

String Methods

  • Python has a set of built-in methods that you can use on strings
  • All string methods return new values.they do not change the original string because the string is an immutable python object.

Following are the most commonly used method for strings are

1. capitalize():

  • The capitalize() method upper case the first letter in the given sequences(string).

Example:

text="hello"
result=text.capitalize()
print(result)

Output:

Hello

2. count():

  • The count() method returns the number of times a specified value appears in the string.
  • Returns the frequency of given element inside string. Syntax:
string.count("Value")

Example:

text1="Hello"
result=text1.count('l')
result1=text1.count('H')

print(result) 
#Result:2

print(result1) 
#Result:1

Output:

1

Search Element Count From Some SPecific Index

Syntax:

string.count(value, start, end)
  1. value:- element which does you want to find count.
  2. start:- Default is 0, starting position of an element.
  3. end:- Default is len(string) ,ending position of an element.

For Example

text1="This is simple Text"
result=text1.count('i')
res=text1.count('i',6,len(text1))
print(result) 
#Result:3

print(res) 
#Result :1

Output:

3
1

3. endswith():

  • The endswith() method returns True if the string ends with the specified value, otherwise False.

Example:

text1="This is simple Text."
result=text1.endswith('.')
print(result)
#Result:True

Output:

True

4. index() :

  • The index() method finds the first occurrence of the specified value.
  • The index() method raises an exception if the value is not found.

Example:

mystring="I am python"
print(mystring.index('a')) 
#Result:2

print(mystring.index('k')) 
#Result:error:-substring not found

Output:

2
Traceback (most recent call last):
  File "/tmp/sessions/7f5718dc77646b8f/main.py", line 5, in <module>
    print(mystring.index('k')) 
ValueError: substring not found

5. find():

  • To find out position of word or character from given string or paragraph use find() method.
  • The find() method finds the first occurrence of the specified value.
  • The find() method returns -1 if the value is not found.

Syntax

string.find(str, startpoint, endpoint)

str:-which string to find startpoint:-the starting index from where you want to find a string endpoint:-Ending index

Example 1:

text="Python"
result=text.find("t")
print(result)
#Result:2

Output:

2

Example 2:

mystring="i love to play cricket and football"
print(mystring.find("o"))
#Result:3

Output:

3

Example 3:

mystring="i love to play cricket and football"
print(mystring.find("cric"))
#Result:15

Output:

15

When you give multiple character string it will find out the starting point of string where it founds.

  • if we want to find out second occurence of of required string
mystring="i love to play cricket and football"
first=(mystring.find("o")) 
second=(mystring.find("o",first+1)) 
print(second)

Output:

8
  • If searching string not present in input string it return -1.
mystring="I am python Expert"
search=mystring.find("this")
print(search)

Output:

-1

To find all occurrence of word

mystring="I am python Expert"
for i,j in enumerate(mystring):
  if j=='t':
    print(i)

Output:

7
17

6. Split()

  • if you want to separate a string with white space or commas or some special character then split method is used
  • split() method returns the list object.
mystring="india pakistan china america"
#split a string with whitespace
splitstring=mystring.split(' ') #or mystring.split()
print(splitstring)

Output:

['india', 'pakistan', 'china', 'america']
  • if you want to split a string and store its values in different variable.
mystring="india pakistan china america"
a,b,c,d=mystring.split(' ')
print(a)
print(b)
print(c)
print(d)

Output:

india
pakistan
china
america

7. join()

  • The join() method takes all items in an iterable and joins them into one string.
  • A string must be specified as the separator.

Syntax:

Sepeartor.join(iterable objects)

Sepeartor: anything that you want to use as separator such as '-',',',''','-', whitespaces, numbers alphabets, special characters

Example:

mylist=['india', 'pakistan', 'china', 'america']
strings=','.join(mylist)
print(strings) 

Output:

india,pakistan,china,america

8. replace()

  • replace() method replaces a specified phrase with another specified phrase.

Syntax:

str.replace('old word','new word')

Example:

stat='kat is good girl'
newstring=stat.replace('is','was')
print(newstring)

Output:

kat was good girl
  • Replace the 'n' first occurrence of the word 'is' Example:
mystring="is he is was he in too is"
newstring=mystring.replace('is','was' ,2)
print(newstring)

Output:

was he was was he in too is

For Checking Data types there are following methods are present

1. isalnum()

  • Returns True if all characters in the string are alphanumeric:
  • True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
  • False if any one character from non alphanumeric value (space)!#%&? Example
mystring="Python3"
Res=mystring.isalnum()
print(Res)

Output:

True

2. isalpha()

  • isalpha() method returns True if all the characters are alphabet letters (a-z).
  • isalpha() method return false if one of the characters is special character or number

Example:

mystring="Python3"
Res=mystring.isalpha()
print(Res)

Output:

False

3. isdecimal()

  • The isdecimal() method returns True if all the characters are decimals (0-9).

example:

mystring="\u0033" #3
res=mystring.isdecimal()
print(res)

Output:

True

4. isdigit()

  • isdigit() method returns True if all the characters are digits, otherwise False.

Example:

number="63663"
res=number.isdigit()
print(res)

Output:

True

Other some remaining methods

isidentifier():

  • Returns True if the string is an identifier

islower():

  • Returns True if all characters in the string are lower case

isnumeric():

  • Returns True if all characters in the string are numeric

isprintable():

  • Returns True if all characters in the string are printable

isspace():

  • Returns True if all characters in the string are whitespaces

istitle():

  • Returns True if the string follows the rules of a title

isupper():

  • Returns True if all characters in the string are upper case