python tidbits* (will update more)

1. Taking spaced input

x = raw_input().split()
## x is a list
print x

2. Taking the integer spaced input

 x = map(int , raw_input().split())
print x

3. Moving a directory upwards, shell command equivalent ‘cd ..’

import os
os.getcwd() ## prints the current working directory
up_path = os.path.split( os.getcwd() )[-2]
os.chdir( up_path )
os.getcwd() ## you will be moved one directory upwards

4. Capitalizing the first letter in every word in a sentence: Split the sentence with white-space as delimiter which returns a list. Here it returns [this, is, wordpress]. Now for each word in the list, capitalized and joined with white space delimiter.

import string
sentence = "this is wordpress"
print ' '.join([string.capitalize(word) for word in sentence.split(' ')])
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s