Initial commit
This commit is contained in:
6
python/String/Calculate the length of a string.py
Normal file
6
python/String/Calculate the length of a string.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def string_length(str1):
|
||||
count = 0
|
||||
for char in str1:
|
||||
count += 1
|
||||
return count
|
||||
print(string_length('sadsadsadasd.com'))
|
||||
@@ -0,0 +1,11 @@
|
||||
def to_uppercase(str1):
|
||||
num_upper = 0
|
||||
for letter in str1[:4]:
|
||||
if letter.upper() == letter:
|
||||
num_upper += 1
|
||||
if num_upper >= 2:
|
||||
return str1.upper()
|
||||
return str1
|
||||
|
||||
print(to_uppercase('Python'))
|
||||
print(to_uppercase('PyThon'))
|
||||
@@ -0,0 +1,4 @@
|
||||
def add_tags(tag, word):
|
||||
return "<%s>%s<!--%s-->" % (tag, word, tag)
|
||||
print(add_tags('i', 'Python'))
|
||||
print(add_tags('b', 'Python Tutorial'))
|
||||
@@ -0,0 +1,6 @@
|
||||
def insert_end(str):
|
||||
sub_str = str[-2:]
|
||||
return sub_str * 4
|
||||
|
||||
print(insert_end('Python'))
|
||||
print(insert_end('Exercises'))
|
||||
@@ -0,0 +1,6 @@
|
||||
def first_three(str):
|
||||
return str[:3] if len(str) > 3 else str
|
||||
|
||||
print(first_three('ipy'))
|
||||
print(first_three('python'))
|
||||
print(first_three('py'))
|
||||
@@ -0,0 +1,5 @@
|
||||
def string_first_half(str):
|
||||
return str[: len(str) / 2]
|
||||
|
||||
print(string_first_half('Python'))
|
||||
print(string_first_half('JavaScript'))
|
||||
@@ -0,0 +1,6 @@
|
||||
def insert_sting_middle(str, word):
|
||||
return str[:2] + word + str[2:]
|
||||
|
||||
print(insert_sting_middle('[[]]', 'Python'))
|
||||
print(insert_sting_middle('{{}}', 'PHP'))
|
||||
print(insert_sting_middle('<<>>', 'HTML'))
|
||||
@@ -0,0 +1,7 @@
|
||||
def reverse_string(str1):
|
||||
if len(str1) % 4 == 0:
|
||||
return ''.join(reversed(str1))
|
||||
return str1
|
||||
|
||||
print(reverse_string('abcd'))
|
||||
print(reverse_string('python'))
|
||||
@@ -0,0 +1,15 @@
|
||||
import textwrap
|
||||
sample_text ='''
|
||||
Python is a widely used high-level, general-purpose, interpreted,
|
||||
dynamic programming language. Its design philosophy emphasizes
|
||||
code readability, and its syntax allows programmers to express
|
||||
concepts in fewer lines of code than possible in languages such
|
||||
as C++ or Java.
|
||||
'''
|
||||
text_without_Indentation = textwrap.dedent(sample_text)
|
||||
wrapped = textwrap.fill(text_without_Indentation, width=50)
|
||||
#wrapped += '\n\nSecond paragraph after a blank line.'
|
||||
final_result = textwrap.indent(wrapped, '> ')
|
||||
print()
|
||||
print(final_result)
|
||||
print()
|
||||
@@ -0,0 +1,5 @@
|
||||
def change_sring(str1):
|
||||
return str1[-1:] + str1[1:-1] + str1[:1]
|
||||
|
||||
print(change_sring('abcd'))
|
||||
print(change_sring('12345'))
|
||||
@@ -0,0 +1,2 @@
|
||||
string = "w3resource.com"
|
||||
print(string.startswith("w3r"))
|
||||
@@ -0,0 +1,27 @@
|
||||
#https://gist.github.com/nchitalov/2f2b03e5cf1e19da1525
|
||||
def caesar_encrypt(realText, step):
|
||||
outText = []
|
||||
cryptText = []
|
||||
|
||||
uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
|
||||
lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
|
||||
|
||||
for eachLetter in realText:
|
||||
if eachLetter in uppercase:
|
||||
index = uppercase.index(eachLetter)
|
||||
crypting = (index + step) % 26
|
||||
cryptText.append(crypting)
|
||||
newLetter = uppercase[crypting]
|
||||
outText.append(newLetter)
|
||||
elif eachLetter in lowercase:
|
||||
index = lowercase.index(eachLetter)
|
||||
crypting = (index + step) % 26
|
||||
cryptText.append(crypting)
|
||||
newLetter = lowercase[crypting]
|
||||
outText.append(newLetter)
|
||||
return outText
|
||||
|
||||
code = caesar_encrypt('abc', 2)
|
||||
print()
|
||||
print(code)
|
||||
print()
|
||||
@@ -0,0 +1,7 @@
|
||||
x = 3000000
|
||||
y = 30000000
|
||||
print("\nOriginal Number: ", x)
|
||||
print("Formatted Number with comma separator: "+"{:,}".format(x));
|
||||
print("Original Number: ", y)
|
||||
print("Formatted Number with comma separator: "+"{:,}".format(y));
|
||||
print()
|
||||
@@ -0,0 +1,11 @@
|
||||
import textwrap
|
||||
sample_text = '''
|
||||
Python is a widely used high-level, general-purpose, interpreted,
|
||||
dynamic programming language. Its design philosophy emphasizes
|
||||
code readability, and its syntax allows programmers to express
|
||||
concepts in fewer lines of code than possible in languages such
|
||||
as C++ or Java.
|
||||
'''
|
||||
print()
|
||||
print(textwrap.fill(sample_text, width=50))
|
||||
print()
|
||||
@@ -0,0 +1,9 @@
|
||||
def not_poor(str1):
|
||||
snot = str1.find('not')
|
||||
sbad = str1.find('poor')
|
||||
|
||||
if sbad > snot:
|
||||
str1 = str1.replace(str1[snot:(sbad+4)], 'good')
|
||||
|
||||
return str1
|
||||
print(not_poor('The lyrics is not that poor!'))
|
||||
@@ -0,0 +1,7 @@
|
||||
x = 0.25
|
||||
y = -0.25
|
||||
print("\nOriginal Number: ", x)
|
||||
print("Formatted Number with percentage: "+"{:.2%}".format(x));
|
||||
print("Original Number: ", y)
|
||||
print("Formatted Number with percentage: "+"{:.2%}".format(y));
|
||||
print()
|
||||
@@ -0,0 +1,6 @@
|
||||
def chars_mix_up(a, b):
|
||||
new_a = b[:2] + a[2:]
|
||||
new_b = a[:2] + b[2:]
|
||||
|
||||
return new_a + ' ' + new_b
|
||||
print(chars_mix_up('abc', 'xyz'))
|
||||
@@ -0,0 +1,7 @@
|
||||
x = 3.1415926
|
||||
y = -12.9999
|
||||
print("\nOriginal Number: ", x)
|
||||
print("Formatted Number with sign: "+"{:+.2f}".format(x));
|
||||
print("Original Number: ", y)
|
||||
print("Formatted Number with sign: "+"{:+.2f}".format(y));
|
||||
print()
|
||||
@@ -0,0 +1,7 @@
|
||||
x = 3.1415926
|
||||
y = 12.9999
|
||||
print("\nOriginal Number: ", x)
|
||||
print("Formatted Number: "+"{:.2f}".format(x));
|
||||
print("Original Number: ", y)
|
||||
print("Formatted Number: "+"{:.2f}".format(y));
|
||||
print()
|
||||
@@ -0,0 +1,7 @@
|
||||
x = 3.1415926
|
||||
y = -12.9999
|
||||
print("\nOriginal Number: ", x)
|
||||
print("Formatted Number with no decimal places: "+"{:.0f}".format(x));
|
||||
print("Original Number: ", y)
|
||||
print("Formatted Number with no decimal places: "+"{:.0f}".format(y));
|
||||
print()
|
||||
@@ -0,0 +1,7 @@
|
||||
x = 3
|
||||
y = 123
|
||||
print("\nOriginal Number: ", x)
|
||||
print("Formatted Number(left padding, width 2): "+"{:0>2d}".format(x));
|
||||
print("Original Number: ", y)
|
||||
print("Formatted Number(left padding, width 6): "+"{:0>6d}".format(y));
|
||||
print()
|
||||
3
python/String/Python program to remove a newline.py
Normal file
3
python/String/Python program to remove a newline.py
Normal file
@@ -0,0 +1,3 @@
|
||||
str1='Python Exercises\n'
|
||||
print(str1)
|
||||
print(str1.rstrip())
|
||||
@@ -0,0 +1,12 @@
|
||||
import textwrap
|
||||
sample_text = '''
|
||||
Python is a widely used high-level, general-purpose, interpreted,
|
||||
dynamic programming language. Its design philosophy emphasizes
|
||||
code readability, and its syntax allows programmers to express
|
||||
concepts in fewer lines of code than possible in languages such
|
||||
as C++ or Java.
|
||||
'''
|
||||
text_without_Indentation = textwrap.dedent(sample_text)
|
||||
print()
|
||||
print(text_without_Indentation )
|
||||
print()
|
||||
@@ -0,0 +1,9 @@
|
||||
def odd_values_string(str):
|
||||
result = ""
|
||||
for i in range(len(str)):
|
||||
if i % 2 == 0:
|
||||
result = result + str[i]
|
||||
return result
|
||||
|
||||
print(odd_values_string('abcdef'))
|
||||
print(odd_values_string('python'))
|
||||
@@ -0,0 +1,7 @@
|
||||
def remove_char(str, n):
|
||||
first_part = str[:n]
|
||||
last_pasrt = str[n+1:]
|
||||
return first_part + last_pasrt
|
||||
print(remove_char('Python', 0))
|
||||
print(remove_char('Python', 3))
|
||||
print(remove_char('Python', 5))
|
||||
6
python/String/Python program to reverse a string.py
Normal file
6
python/String/Python program to reverse a string.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def reverse_string(str1):
|
||||
return ''.join(reversed(str1))
|
||||
print()
|
||||
print(reverse_string("abcdef"))
|
||||
print(reverse_string("Python Exercises."))
|
||||
print()
|
||||
@@ -0,0 +1,16 @@
|
||||
import textwrap
|
||||
sample_text ='''
|
||||
Python is a widely used high-level, general-purpose, interpreted, dynamic
|
||||
programming language. Its design philosophy emphasizes code readability,
|
||||
and its syntax allows programmers to express concepts in fewer lines of
|
||||
code than possible in languages such as C++ or Java.
|
||||
'''
|
||||
|
||||
text1 = textwrap.dedent(sample_text).strip()
|
||||
print()
|
||||
print(textwrap.fill(text1,
|
||||
initial_indent='',
|
||||
subsequent_indent=' ' * 4,
|
||||
width=80,
|
||||
))
|
||||
print()
|
||||
@@ -0,0 +1,5 @@
|
||||
def lexicographi_sort(s):
|
||||
return sorted(sorted(s), key=str.upper)
|
||||
|
||||
print(lexicographi_sort('w3resource'))
|
||||
print(lexicographi_sort('quickbrown'))
|
||||
@@ -0,0 +1,8 @@
|
||||
def strip_chars(str, chars):
|
||||
return "".join(c for c in str if c not in chars)
|
||||
|
||||
print("\nOriginal String: ")
|
||||
print("The quick brown fox jumps over the lazy dog.")
|
||||
print("After stripping a,e,i,o,u")
|
||||
print(strip_chars("The quick brown fox jumps over the lazy dog.", "aeiou"))
|
||||
print()
|
||||
Reference in New Issue
Block a user