Data Warehousing, BI and Data Science

27 November 2020

Python: String and Array

Filed under: Data Warehousing — Vincent Rainardi @ 4:23 am

To remove spaces from the left or right, use strip, lstrip and rstrip functions.

Example: a = " Test%%"
print(a.lstrip().rstrip("%"), a.strip("%").strip())
Output: Test Test

To remove all spaces (including in the middle) use the replace function.

Example: b = " %Te st %% A"
print(b.replace(" ","").replace("%",""))
Output: TestA

To split string use the split function.

Example: c = "A,B,C"
d = c.split(",")
print(d, d[1])
Output: ['A', 'B', 'C'] B

To join strings use the join function.

Example: e = " & ".join(d)
Output: A & B & C

To declare and initialise 3 variables at the same time use commas:

Example: a,b,c = "A", "B", "C"

To get a portion of the string use [a:b:c], which means from position a to position b, skipping c characters (a or b or c can be omitted).
Note: including b (b is included).
A negative index means counting from the right. The right most character is -1.

Example: a = "0123456789"
print(a[0:4], a[1:7:2], a[-2])
Output: 0123 135 8

Example: print(a[:5], a[:], a[::3])
Output: 01234 0123456789 0369

Example: a = "I love Python programming"
print(a[7:13], a[-18:-12], len(a))
Output: Python Python 25

Arithmetic operators are +, -, /, %, **, //
(the last 3 are modulo, power, floor division)

Example: a,b = 5,2
print(a%b, a**b, a//b)
Output: 1 25 2

Comparison operators are >, <, ==, !=

Example: print(a>b, a<b, a==b, a!=b)
Output: True False False True

The print function accepts parameters.

Example: a,b = "A","B"
print("{0} test {1}".format(a,b))
Output: A test B

To concatenate 2 strings use the + operator.

Example: print(a + " & " + b, "Line1\nLine2")
Output: A & B Line1

To print a double quote, enclose the string with a single quote.
Or escape it with a backslash.

Example: print('a"b', "a\"b")
Output: a"b  a"b

To print a single quote, inclose the string with a double quote.
Or escape it with a backslash.

Example: print("a'b", 'a\'b')
Output: a'b   a'b

To change line use \n.
If we use r (means raw string), \n doesn’t have an affect.

Example: print(r"a\n")
Output:  a\n

Set means distinct members.

Example: a = set('aba')
Output: {'a', 'b'}

Set operations are -,|,&,^.

  • a-b: in a but not in b.
  • a|b: in a or b or both.
  • a&b: in both a and b.
  • a^b: in a or b but not both.
Example: a,b = set('ab'), set('ac')
print(a-b, a|b, a&b, a^b)
Output: {'b'} {'a', 'b', 'c'} {'a'} {'b', 'c'}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

Blog at

%d bloggers like this: