Skip to main content

6.Python String Methods

STRING METHODS 
    Python includes the following built-in methods to manipulate strings which can be explained as follows. 

1.len() 
    String length is used to find the length of the string. 
Syntax: 
len(variable_name) or len(“string”) 
Program
a="TamilHacks" 
print(len(a)) 
print(len("abcd")) 
Output
10 
2.capitalize() 
    String capitalize is used to change the first character of string into upper case.Note that the first character is converted into uppercase and the remaining characters in converted to lower case. 
Syntax: 
Variable.capitalize() 
Program
a="tamilHacks" 
print(a.capitalize()) 
Output: 
Tamilhacks 
3.casefold() 
    String casefold is used to convert the strings to lower case. 
Program: 
a="TAMILHACKS" 
print(a.casefold()) 
Output: 
Tamilhacks 
4.center() 
    The string method center() is used to return a centered string.It takes one argument. Here the word "TamilHacks", takes up the space of 30 characters, with "TamilHacks" in the middle 
Program
a="Welcome to TamilHacks" 
print(a.center(30)) 
print(a.center(30,'x')) 
Output 
Welcome to TamilHacks 
xxxxWelcome to TamilHacksxxxxx 
5.count() 
    The string count is used to print the number a times a specified character /value occurred in the string. It is also possible to count the value within the specified position 
Program
a="Welcome to tamilHacks" 
print("to occurs",a.count("to"),"time") 
print("e occurs",a.count('e'),"times") 
print("t occurs",a.count("t",3,15),"times within these positions") 
Output
to occurs 1 time 
e occurs 2 times 
t occurs 2 times within these positions 
6.encode() 
    The string encode method is used to print the encoded version of the string. If the encoding is not specified, UTF-8 will be used. It is possible to specify the encoding and error values.The error values includes the following 
  • Backslashreplace-replaces backslash with the character that cannot be encoded
  • Ignore-just ignores the character that cannot be encoded 
  • Namereplace-replaces the character with a text explaining the  character
  • Strict -produces the error if the character cannot be encoded.
  • Replace-replace the character that cannot be encoded with a question mark 
  • Xmlcharrefreplace-replaces the character that cannot be encoded with a xml character
Program
a="Welcome to tåmilHacks!!" 
print(a.encode()) 
print(a.encode(encoding="ascii",errors="backslashreplace")) 
print(a.encode(encoding="ascii",errors="ignore")) 
print(a.encode(encoding="ascii",errors="namereplace")) 
print(a.encode(encoding="ascii",errors="replace")) 
print(a.encode(encoding="ascii",errors="xmlcharrefreplace")) 
print(a.encode(encoding="ascii",errors="strict")) 
Output:
b'Welcome to t\xc3\xa5milHacks!!' 
b'Welcome to t\\xe5milHacks!!' 
b'Welcome to tmilHacks!!' 
b'Welcome to t\\N{LATIN SMALL LETTER A WITH RING ABOVE}milHacks!!' 
b'Welcome to t?milHacks!!' 
b'Welcome to tåmilHacks!!' 
Traceback (most recent call last): 
File "main.py", line 8, in <module> 
print(a.encode(encoding="ascii",errors="strict")) 
UnicodeEncodeError: 'ascii' codec can't encode character '\xe5' in position 12: ordinal not in range(128) 
7.endswith() 
    The endswith() string method is used to find whether the string ends with the specified value.It return true,if it ends with the specified value,else it returns false. 
Program
a="Welcome to tåmilHacks!!" 
print(a.endswith('s')) 
print(a.endswith('!')) 
Output
False 
True 
8.upper() 
    String upper() is used to convert the string to uppercase. 
Program
a="tamilhacks" 
print(a.upper()) 
Output: 
TAMILHACKS 
9.swapcase() 
    String method swapcase() is used to change the upper case to lower case and lower case to upper case. 
Program
a="TamilHacks" 
print(a.swapcase()) 
Output 
tAMILhACKS 
10.startswith() 
    The startswith() string method is used to find whether the string starts with the specified value.It return true,if it starts with the specified value,else it returns false. 
Program
a="Welcome to tåmilHacks!!" 
print(a.startswith('s')) 
print(a.startswith('W')) 
Output 
False 
True
11.expandtabs()
    The expandtabs() method is used to set the tab size for that particular string.\t denotes the tab.
Program
a="Tamil\tHacks"
b=  a.expandtabs(10)
print(b)
Output 
Tamil       Hacks

12.isdigit()
    The isdigit() method is used to check whether the string is a digit or not.It returns true if the string is digit else it returns false.
Program
a="TamilHacks"
b= "1000920"
c=a.isdigit()
d=b.isdigit()
print(c)
print(d)
Output 
False
True
These are some of the string methods in python.
Check out the video below to more about python and string methods.

Happy Learning!!






Tamil     Hacks
Tamil     Hacks























Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl