Showing posts with label string formatting. Show all posts
Showing posts with label string formatting. Show all posts

Wednesday, November 4, 2020

String Formatting-3

 String Formatting-3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1. endswith()-to check whether string ends with a particular character(case sensitive)

-->print('numpy'.endswith('y'))

-->True

2. find()/index()- method finds the first occurrence of the specified value,returns -1 if not found,almost like index method.

-->print('Debug'.find('b'))

-->2

-->print('Debug'.find('s'))

-->-1

-->print('Debug'.index('s'))

-->ValueError



3. replace()- This method replaces a specified phrase with another specified phrase.

-->print('hi there'.replace('hi','hello'))

-->hello there



4. split()-splits a string into list,based on a seprator(default string).

-->print('harsh'.split())

-->['harsh']

-->print('h,a,r,s,h'.split(','))

-->['h','a','r','s','h']



5. join()-joins a iterable into string,using a seprator(default string).

-->print(','.join(['h','a','r','s','h']))

-->'h,a,r,s,h

String Formatting-2

 String Formatting-2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1. isalpha()-to check whether string has only alphabets

-->print('alpha'.isalpha())

-->True



2. isdigit()-to check whether string has only digits

-->print('1010'.isdigits())

-->True



3. isspace()-to check whether string has only spaces

-->print('   '.isspace())

-->True



4. center()-this will center align the string ,using specified character(space is default) as a fill character

-->print('Python'.center(10,'*'))

-->**Python**



5. startswith()-to check whether string starts with a particular character(case sensitive)

-->print('numpy'.startswith('n'))

-->True

String Formatting-1


String Formatters

Part -1

Dear Readers ,
today I would like to share about string formatter in python and I have them all in this single blog,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Important string operations-1

1. len()-to evaluate the length of a string

-->print(len('sample'))

-->6



2. str()-create new string object from given object

-->print(type(str(6)))

-->'<class 'str'>'



3. lower()-to covert a string to lowercase

-->print('HI'.lower())

-->hi



4. upper()-to covert a string to uppercase

-->print('hi'.upper())

-->HI



5. strip()-strip the spaces in input there are two subtype's lstrip and rstrip also

-->print('  Python  '.strip())

-->Python

-->print('  Python'.lstrip())

-->Python

-->print('Python  '.rstrip())

-->Python

Linear Search algorithm

Linear Search Algorithm Dear Readers, In this Blog today I would Share about Linear Search Algorithm a must know for everyone whether you wa...