Python Text Basics Flashcards

1
Q

What are F String Literals ?

A

Format String Literals , are used to format strings in print method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to present dictionary value by using F String ?

A

d = {‘a’:123,’b’:456}

print(f”My No. is - {d[‘a’]}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to present list value by using F String ?

A

mylist = [0,1,2,3]

print(f”My No. is - {mylist[0]}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to create Tuple ?

A

myFruits = (‘Apple’,’Banana’,’Grapes’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to create List of Tuples ?

A

books = [(‘Author’,’Title’,’Pages’),(‘Twain’,’Rafting’,601),(‘Hamilton’,’Mythology’,144)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

*IMP

How to unpack tuples from List ?

A

books = [(‘Author’,’Title’,’Pages’),(‘Twain’,’Rafting’,601),(‘Hamilton’,’Mythology’,144)]+

for author,title,pages in books:
print(f”{author} {title} {pages}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to fit numerical value with string in f string ?

A

profiles = [(‘Name’,’Age’),(‘Hemant Kumar ‘,40)]
for name,age in profiles:
print(f”{name:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to set the padding between output of two variables with fstring ?

A

name = ‘Hemant Kumar’
age = 32
print(f”{name:{10}} {age:{5}}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

*IMP

How to Print line with variable output using fstring ?

A

name = ‘Hemant Kumar’
age = 32
print(f”{name:-{5}}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to Create a Data time Object?

A

from datetime import datetime

today = datetime(year=2019,month=2,day=28)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the website which gives reference to format time ?

A

The website is strtime.org

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to format date object with print method ?

A

from datetime import datetime
today = datetime(year=2019,month=2,day=28)
print(f”{today:%B %d,%Y}”)

B for Full Month
d for Day
Y for Full Year

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to create text file in Jupiter Notebook ?

A

%%writefile test.txt
Hello, this is test file,
This is second line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to get current Absolute Path ?

A

using pwd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to create a file object to read/write a file ?

A

We have to use open() method .

myfile = open(‘test.txt’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

File name in Python ,Windows, Linux, MacOS

A

We have to use double backslash in Windows, & forward slash in Linuc/MacOS

Windows :
‘C:\Users\cs1\MEGAsync\PythonCoding
Linux : /home/hemant/

17
Q

How to read a text file ?

A

myfile.read()

18
Q

What happens if we read text files by two times ?

myfile. read()
myfile. read()

A

I we read file first time , the curson goes to end of file , & we read file second time it shows nothing, because cursor is in the end of file .

19
Q

How to move file cursor at index position zero or at beginning of file?

A

myfile.seek(0)

20
Q

How to save content of file in variable ?

A

content = myfile.read()

print(content)

21
Q

What are literals in python?

A

Literals are the constant values assigned to the constant variables

22
Q

How to close a text file ?

A

myfile.close()

23
Q

How to create a list of lines from text file ?

A
myfile = open('test.txt')
mylines = myfile.readlines()
for line in mylines:
    print(line)
myfile.close()
24
Q

How to split/fetch first word from lines

in text file ?

A
myfile = open('test.txt')
mylines = myfile.readlines()
for line in mylines:
    print(line.split()[0])
myfile.close()
25
Q

How to present first char of each line from text file ?

A
myfile = open('test.txt')
mylines = myfile.readlines()
for line in mylines:
    print(line[0])
myfile.close()
26
Q

What is the default mode of file open method() ?

A

The default mode is read mode ?

27
Q

How to to get descriptive help in Jupyter Notebook Cell while typing a Code ?

A

Use keyboard shortcut shift + tab , or shift + double tab for more decription

28
Q

How to open text file for writing & reading ?

A

We have to pass w+ parameter to open() method in quotes.

myfile = open(‘test.txt’,’w+’)

29
Q

What are the precautions while using w+ parameter ?

A

It deletes the existing contents of file ?

30
Q

How to open a file with append mode ?

A
We have to use a+ parameter .
myfile = open('test1.txt','a+') 
or myfile = open('test1.txt', 'mode='a+')
myfile.write('\nThis is second line')
myfile.close()
myfile.read()
31
Q

How to append to new line ?

A
myfile = open('test1.txt',mode='a+')
myfile.write('\nThis is second line')
myfile.close()
myfile =open('test1.txt')
print(myfile.read())
32
Q

What is context manager, & we use it?

A

we use with keyword as context manager which automatically closes the file

with open('test1.txt',mode='r') as mynewfile:
    lines=mynewfile.readlines()
for line in lines:
    print(line)
33
Q

Which Library is used for pdf files ?

A

PyPDF2

34
Q

which library is used for regular expressions

A

re library used in Python for regular expression

35
Q

how to search first instance only

A

string = ‘his name is hemant, it section hemant’

re.search(r’hemant’,string)

36
Q

how to search all occurrences in a string

A

we have to use findall method

text = “my phone is a new phone”

matches = re.findall(“phone”,text)

matches

37
Q

how to use for loop with re

A

we have to use finditer method

text = “my phone is a new phone”
for match in re.finditer(“phone”,text):
print(match.span()) military parade