p

python tutorial - Python String | Python String Methods | Part 3 - strip, lstrip, rstrip, min, max, maketrans, translate & relplace - learn python - python programming



 python stripreplace

Learn Python - Python tutorial - python stripreplace - Python examples - Python programs

1. strip() :- This method is used to delete all the leading and trailing characters mentioned in its argument.

Python - strip

2. lstrip() :- This method is used to delete all the leading characters mentioned in its argument.

Python - lstrip

3. rstrip() :- This method is used to delete all the trailing characters mentioned in its argument.

Python - rstrip

python - Sample - python code :

# Python code to demonstrate working of 
# strip(), lstrip() and rstrip()
str = "---wikitechy---"
 
# using strip() to delete all '-'
print ( " String after stripping all '-' is : ", end="")
print ( str.strip('-') )
 
# using lstrip() to delete all trailing '-'
print ( " String after stripping all leading '-' is : ", end="")
print ( str.lstrip('-') )
 
# using rstrip() to delete all leading '-'
print ( " String after stripping all trailing '-' is : ", end="")
print ( str.rstrip('-') )
click below button to copy the code. By Python tutorial team

python programming - Output :

String after stripping all '-' is : wikitechy
String after stripping all leading '-' is : wikitechy---
String after stripping all trailing '-' is : ---wikitechy

4. min(“string”) :- This function returns the minimum value alphabet from string.

5. max(“string”) :- This function returns the maximum value alphabet from string.

 python strip

Learn Python - Python tutorial - python strip - Python examples - Python programs

python - Sample - python code :

# Python code to demonstrate working of 
# min() and max()
str = "wikitechy"
 
# using min() to print the smallest character
# prints 'e'
print ("The minimum value character is : " + min(str));
 
# using max() to print the largest character
# prints 'y'
print ("The maximum value character is : " + max(str));
click below button to copy the code. By Python tutorial team

python programming - Output :

The minimum value character is : e
The maximum value character is : y

6. maketrans() :- It is used to map the contents of string 1 with string 2 with respective indices to be translated later using translate().

7. translate() :- This is used to swap the string elements mapped with the help of maketrans().

python - Sample - python code :

# Python code to demonstrate working of 
# maketrans() and translate()
from string import maketrans # for maketrans()
 
str = "wikitechy"
 
str1 = "ich"
str2 = "abc"
 
# using maktrans() to map elements of str2 with str1
mapped = maketrans( str1, str2 );
 
# using translate() to translate using the mapping
print "The string after translation using mapped elements is : "
print  str.translate(mapped) ;
click below button to copy the code. By Python tutorial team

python programming - Output :

The string after translation using mapped elements is : 
wakatebcy

In the above code, ‘g’ is replaced by a, ‘f’ is replaced by b and ‘o’ is replaced by ‘c’ in the string using translate function.

8. replace() :- This function is used to replace the substring with a new substring in the string. This function has 3 arguments. The string to replace, new string which would replace and max value denoting the limit to replace action ( by default unlimited ).

python - Sample - python code :

# Python code to demonstrate working of 
# replace()
 
str = "intertechy is a e-learning website inter"
 
str1 = "inter"
str2 = "wiki"
 
# using replace() to replace str2 with str1 in str
# only changes 1 occurrences 
print ("The string after replacing strings is : ", end="")
print (str.replace( str1, str2, 2)) 
click below button to copy the code. By Python tutorial team

python programming - Output :

The string after replacing strings is : wikitechy is a e-learning website inter

Wikitechy tutorial site provides you all the learn python , learn to program in python , python coding class , learn python programming language online , learn python coding , learn how to code python , free python course online , python training online free , python book , python developer , python online course free

Related Searches to Python String Methods | Part 3 - strip, lstrip, rstrip, min, max, maketrans, translate and replace