Showing posts with label python_string. Show all posts
Showing posts with label python_string. Show all posts

Sep 17, 2019

python str vs repr vs eval

import datetime 

foo = 100
var = 'foo'

print(var) #foo

#with quotes
print(repr(var)) #'foo'

#evaluates any variables
print(eval(var)) #100

today = datetime.datetime.now()

# Prints readable format for date-time object
print (str(today)) #2019-09-17 11:54:13.675979

# prints the official format of date-time object
print (repr(today)) #datetime.datetime(2019, 9, 17, 11, 54, 13, 675979)

Jan 26, 2019

Python reverse a string

Python reverse a string

ss = 'Hello World'
print ss[::-1] #dlroW olleH
print reversed(ss)
print ''.join(reversed(ss)) #dlroW olleH



O/P:
dlroW olleH 
<reversed object at 0x7f5813ce73d0> 
dlroW olleH




Jun 23, 2018

Learning Strings in Python

#!/usr/local/bin/python2.7

'''
#Learning Strings in Python
    File name: string_all_operations.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

'''
Python Strings are immutable
Python uses single quotes ' double quotes " and triple quotes """
['capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
'''

string = "india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world."
print string

#The capitalize() function returns a string with the first character in capital.
#Immutable
string_cap = string.capitalize()
print string_cap
print string

print '---------------Slice---------------'
print string[0:20]
print string[-5:]

#india also called th
#orld.

print '---------------Forming String with variables---------------'
print "The item {} is repeated {} times".format('test',100)
#The item test is repeated 100 times

print "The item %s is repeated %d times"% ('test',100)
#The item test is repeated 100 times

# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
#Hello Adam, your balance is 230.2346.

# positional arguments
print("Hello {0}, your balance is {2}.".format("Adam", 230.2346, 100))
#Hello Adam, your balance is 230.2346.

# keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
#Hello Adam, your balance is 230.2346.

# mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
#Hello Adam, your balance is 230.2346.

#print("{:2.2f}".format(234567.2346768778))
print("{:.3f}".format(12.2346768778))
#12.235

print '---------------check if Substring Exists---------------'
print 'democracy' in string
#True

print '---------------String Copy---------------'
string_1 = string
string_1 += ' India is great'
print string
print string_1
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world. India is great

print '---------------String Upper/Lower Case---------------'
print string_1.upper()
print string_1.lower()

#INDIA ALSO CALLED THE REPUBLIC OF INDIA IS A COUNTRY IN SOUTH ASIA. INDIA IS THE SEVENTH-LARGEST COUNTRY BY AREA, THE SECOND-MOST POPULOUS COUNTRY (WITH OVER 1.2 BILLION PEOPLE), AND THE MOST POPULOUS DEMOCRACY IN THE WORLD. INDIA IS GREAT
#india also called the republic of india is a country in south asia. india is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world. india is great

print '---------------String Strip---------------'
string_2 = '   India is a democratic country       '
print string_2
string_2 =  string_2.strip()
print string_2

#   India is a democratic country       
#India is a democratic country

print '---------------String Split---------------'
print string
lista = string.split('country')
print lista

#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.
#['india also called the Republic of India is a ', ' in South Asia. India is the seventh-largest ', ' by area, the second-most populous ', ' (with over 1.2 billion people), and the most populous democracy in the world.']

print '---------------String Join---------------'
print lista
print "," .join(lista)
#print string

#['india also called the Republic of India is a ', ' in South Asia. India is the seventh-largest ', ' by area, the second-most populous ', ' (with over 1.2 billion people), and the most populous democracy in the world.']
#india also called the Republic of India is a , in South Asia. India is the seventh-largest , by area, the second-most populous , (with over 1.2 billion people), and the most populous democracy in the world.

print '---------------String Title---------------'
print string.title()
#India Also Called The Republic Of India Is A Country In South Asia. India Is The Seventh-Largest Country By Area, The Second-Most Populous Country (With Over 1.2 Billion People), And The Most Populous Democracy In The World.

print '---------------String find---------------'
print string
print string.find('country') #returns lowest index
print string.rfind('republic') #returns highest index, if not found, retunns -1
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.
#45
#-1

print '---------------String functions---------------'
print string.capitalize()
#India also called the republic of india is a country in south asia. india is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.

print string.count('country') #No of times subsrting has repeated
#3

print string.isspace() #returns true if there are only whitespace characters
#False

string_2 = string.swapcase()
print string_2
#INDIA ALSO CALLED THE rEPUBLIC OF iNDIA IS A COUNTRY IN sOUTH aSIA. iNDIA IS THE SEVENTH-LARGEST COUNTRY BY AREA, THE SECOND-MOST POPULOUS COUNTRY (WITH OVER 1.2 BILLION PEOPLE), AND THE MOST POPULOUS DEMOCRACY IN THE WORLD.

print string_2.swapcase()
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.

print string.center(250, '-')
#-------------india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.-------------