• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Python Array String

November 18, 2014 by Dhaval Dave

Arrays String

As we declare string, we can use it as Array of String.
string = ‘GoHired’
print string[0]
>> G

PS :  To show OutPut we have used  >>  sign
string = ‘GoHired’
print ‘string at 0 ‘ + string[0]
print ‘string at 6 ‘ + string[6]
>>string at 0 G
>>string at 6 d
To print String from back, just use negative indexes.
example
string = ‘GoHired’
print ‘string at -1 ‘ + string[-1]
print ‘string at -2 ‘ + string[-2]
print ‘string at -3 ‘ + string[-3]
>> string at -1 d
>> string at -2 e
>> string at -3 r
Architecture of String
 +—+—+—+—+—+—+—+
 | G | o | H | i | r | e | d |
 +—+—+—+—+—+—+—+
 0   1   2   3   4   5   6  7
-7  -6  -5  -4  -3  -2  -1  0
 

SubString

print ‘string between 0 to 3 ‘ + string[0:3]
# characters from position 0 (included) to 3 (excluded)
>> GoH
Python strings cannot be changed — they are immutable
string[0] = ‘D’ 
#Error 
TypeError: ‘str’ object does not support item assignment
If you need a different string, you should create a new one:
print ‘D’ + string [1:]
>> DoHired

print 'Home' + string [:2]
>> HomeGo

Length Function

print len(string)
x = len(string)
print “string[0:len(string)] = ” + string[0:len(string)]
print “string[0:x] = ” + string[0:x]
>> 7
>> string[0:len(string)] = GoHired
>> string[0:x] = GoHired
Other Functions and its OutPuts

capitalize() & lower()

# capitalize() : First character capitalized and the rest lowercased.
# lower() : Converts all uppercase letters in string to lowercase
str=’inDia’
print str.capitalize()
>> India
# See ‘D’

center(width, fillchar)

Returns a space-padded string with the original string centered to a total of width columns
str=’india’
print “str.center(10, ‘*’) : “, str.center(10, ‘*’)
>> str.center(10, '*') :  **india***
 

count(str, start,end)

Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given
str = “This is GoHired Site !!!”;
sub = “i”;
print “str.count(“+ sub + “, 4, 40) : “, str.count(sub, 4, 40)
sub = “DD”;
print “str.count(“+ sub +”) : “, str.count(sub)
>> str.count(i, 4, 40) :  3
>> str.count(DD) :  0

Encode Decode(encoding=’UTF-8′,errors=’strict’)

errors — This may be given to set a different error handling scheme. The default for errors is ‘strict’, meaning that encoding errors raise a UnicodeError.
Other possible values are ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ and any other name registered via codecs.register_error().
str = “This is GoHired Site !!!”;
str = str.encode(‘base64′,’strict’);
print “Encoded String: ” + str;
print “Decoded String: ” + str.decode(‘base64′,’strict’)
>> Encoded String: VGhpcyBpcyBHb0hpcmVkIFNpdGUgISEh
>> Decoded String: This is GoHired Site !!!

Other Encoding list : https://docs.python.org/2/library/codecs.html#standard-encodings

Find(str, beg,end)

if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise
str1 = “This is GoHired Site !!!”;
str2 = “Go”;
print str1.find(str2);
print str1.find(str2, 5);
print str1.find(str2, 30);

>> 8
>> 8
>> -1

join(sequence)

str = ” “;
seq = (“Welcome”,”to” ,”Go”,”Hired”); # This is sequence of strings.
print str.join( seq );

>> Welcome to Go Hired

maketrans() & translate(trantab)

maketrans() : Returns a translation table to be used in translate function.
translate(table, deletechars=””) Translates string according to translation table str(256 chars), removing those in the del string

from string import maketrans   # Required to call maketrans function.
intab = “aeiou”
outtab = “12345”
trantab = maketrans(intab, outtab)
str1 = “Welcome to Go Hired !!!”;
print str1.translate(trantab);
print str1.translate(trantab, ‘xm’); 

>> W2lc4m2 t4 G4 H3r2d !!!
>> W2lc42 t4 G4 H3r2d !!!

You can see running functions at  : http://ideone.com/OpFECC

Similar Articles

Filed Under: problem, Uncategorized

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find next greater number with same set of digits

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Implement LRU Cache

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Maximum occurred Smallest integer in n ranges

Sort Stack in place

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Microsoft BING Interview Experience

Facebook Interview Question : Interleave List

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Maximum size of square sub matrix with all 1’s in a binary matrix

VMWare Openings

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Trapping Rain Water

CodeChef’ RRCOPY

The greedy coins game Dynamic Programming

Closed Parentheses checker

Skiing on Mountains Matrix

Doubly linked list

Reverse a Linked List in groups of given size

Printing intermediate Integers between one element & next element of array

Printing each word reverse in string

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

K’th Largest Element in BST when modification to BST is not allowed

Longest Increasing Subsequence

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Generate next palindrome number

Sequence Finder Dynamic Programming

Find and print longest consecutive number sequence in a given sequence in O(n)

Generic Object Oriented Stack with Template

Copyright © 2023 · Genesis Framework · WordPress · Log in