Loading...

Python tutorial

Python Strings

A string is a value made up of one or more characters, surrounded by a single or double quotes.

lets create a new file and name it as demo_strings.py and write this below code

demo_strings.py

message = "i love kwikl3arn"
print(message)

Execute the code in following below manner.

C:\Users\My Name>python demo_strings.py
i love kwikl3arn

both single and double quotes work for strings, so we can use quotation marks inside a string.

demo_strings_quote.py

my_quote='I said, "i love kwikl3arn"'
print(my_quote)

Execute the code in following below manner.

C:\Users\My Name>python demo_strings_quote.py
I said,"i love kwikl3arn"

Insert tabs and new lines

insert tabs and new lines into string by useing special squences \t and \n

 demo_strings_tab_newline.py

my_quote='program languages:\n\t python \n\t java'
print(my_quote)

Execute the code in following below manner.

C:\Users\My Name>python demo_strings_tab_newlines.py
program languages:
	 python 
	 java

Note: there is no limit on the length of a string


variables in strings

in python 3.6 inwards,we can use a variable directly inside  a string

demo_strings2.py

username = 'dilip'
print(f"welcome back,{usrname}!")

Execute the code in following below manner.

C:\Users\My Name>python demo_strings2.py
welcome back, dilip!

Here is the short for dormat and tells the python to insert a value of the variable listed in bracess insed the string,This tells python to formst the string useing the given variables value.

this is fine then what about python 3.6 below,we must use the format() method

demo_string_format.py

mytime='5PM'
message="Time is,{}".format(mytime)
print(message)

Execute the code in following below manner.

C:\Users\My Name>python demo_strings_format.py
Time is,5PM

The placeholder {} will be replaced by the forst variable include in the call to format().we can include many variables as per the requirement

demo_strings_format2.py

username='dilip'
mytime='5PM'
message="welcome,{} move time at {}".format(username,mytime)
print(message)

Execute the code in following below manner.

C:\Users\My Name>python demo_strings_format2.py
welcome, dilip movie time at 5PM

String Methods

A string method is a function that performs an action on a string.

title()

name='dilip kumar'
print(name.title())

Execute the code in following below manner.

C:\Users\My Name>python demo_title.py
Dilip Kumar

upper()

name='dilip kumar'
print(name.upper())

Execute the code in following below manner.

C:\Users\My Name>python demo_upper.py
DILIP KUMAR

 lower()

name='Dilip KumaR'
print(name.lower())

Execute the code in following below manner.

C:\Users\My Name>python demo_lower.py
dilip kumar

 Removeing white space

lstrip()

name=' dilip'
print(name.lstrip())

Execute the code in following below manner.

C:\Users\My Name>python demo_lstrip.py
dilip

 rstrip()

name=' dilip '
print(name.rstrip())

Execute the code in following below manner.

C:\Users\My Name>python demo_rstrip.py
 dilip

 strip()

name=' dilip '
print(name.strip())

Execute the code in following below manner.

C:\Users\My Name>python demo_strip.py
dilip

these methods are usefull for presenthing data in a certain format or cleaning up user submitted data