Search This Blog

Sunday, October 29, 2017

Python String and Loop methods

Python Sting methods can be used for file automation split(), lsplit, rsplit and other methods. For expample with lsplit('1234567890') method the numbers from the beginning of files name can removed.

With interactive examples you can use test methods https://www.programiz.com/python-programming/methods/string/

Python String Methods
Python has quite a few methods that string objects can call to perform frequency occurring task (related to string). For example, if you want to capitalize the first letter of a string, you can use capitalize() method.

The page contains all methods of string objects. Also, the page includes built-in functions that can take string as a parameter and perform some task. For example, len() method returns the length of the string passed as a parameter to this function.


Python String Method Description
capitalize() Converts first character to Capital Letter
center() Pads string with specified character
casefold() converts to casefolded strings
count() returns occurrences of substring in string
endswith() Checks if String Ends with the Specified Suffix
expandtabs() Replaces Tab character With Spaces
encode() returns encoded string of given string
find() Returns the Highest Index of Substring
format() formats string into nicer output
index() Returns Index of Substring
isalnum() Checks Alphanumeric Character
isalpha() Checks if All Characters are Alphabets
isdecimal() Checks Decimal Characters
isdigit() Checks Digit Characters
isidentifier() Checks for Valid Identifier
islower() Checks if all Alphabets in a String are Lowercase
isnumeric() Checks Numeric Characters
isprintable() Checks Printable Character
isspace() Checks Whitespace Characters
istitle() Checks for Titlecased String
isupper() returns if all characters are uppercase characters
join() Returns a Concatenated String
ljust() returns left-justified string of given width
rjust() returns right-justified string of given width
lower() returns lowercased string
upper() returns uppercased string
swapcase() swap uppercase characters to lowercase; vice versa
lstrip() Removes Leading Characters
rstrip() Removes Trailing Characters
strip() Removes Both Leading and Trailing Characters
partition() Returns a Tuple
maketrans() returns a translation table
rpartition() Returns a Tuple
translate() returns mapped charactered string
replace() Replaces Substring Inside
rfind() Returns the Highest Index of Substring
rindex() Returns Highest Index of Substring
split() Splits String from Left
rsplit() Splits String From Right
splitlines() Splits String at Line Boundaries
startswith() Checks if String Starts with the Specified String
title() Returns a Title Cased String
zfill() Returns a Copy of The String Padded With Zeros
format_map() Formats the String Using Dictionary
Python any() Checks if any Element of an Iterable is True
Python all() returns true when all elements in iterable is true
Python ascii() Returns String Containing Printable Representation
Python bool() Coverts a Value to Boolean
Python bytearray() returns array of given byte size
Python bytes() returns immutable bytes object
Python compile() Returns a Python code object
Python complex() Creates a Complex Number
Python enumerate() Returns an Enumerate Object
Python filter() constructs iterator from elements which are true
Python float() returns floating point number from number, string
Python input() reads and returns a line of string
Python int() returns integer from a number or string
Python iter() returns iterator for an object
Python len() Returns Length of an Object
Python max() returns largest element
Python min() returns smallest element
Python map() Applies Function and Returns a List
Python ord() returns Unicode code point for Unicode character
Python reversed() returns reversed iterator of a sequence
Python slice() creates a slice object specified by range()
Python sorted() returns sorted list from a given iterable
Python sum() Add items of an Iterable
Python zip() Returns an Iterator of Tuples

With search "pypi bulk file renaming", there are two projects to automate bulk file renames
batch-rename 1.0.6 : Python Package Index
rename 1.2 : Python Package Index 
Search "github file bulk file renaming: first page bring 5-7 projects that you can use.

Today I coded from shared Youtube videos two files renaming automation codes. What for I need? To understand and to program integrated or tailored solutions to automate codes. Today I tested also argparse library with cmd parameters.

With search " python github automate first file in folder in selection" I found interesting project to download from cmd Youtube videos.
youtube-dl - download videos from youtube.com or other video platforms. Not test yet.; AND
autopep8 1.3.3 PEP8 is code layout and notes standard which programmers use. Like code line shall not exceed 79 characters.
autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the

Search "github python copy files in sequence" shows "download files and renames them in sequence · GitHub". So I looked at code and copy code which rename files from first one file till end of folder.
I will adapt code if needed and use to automate files - numerate and move first, numerate and move first, there fore I will need write loop for this and other tasks functions . This code uses files list iteration.

def read_in():
lines = sys.stdin.readlines()
for i in range(len(lines)):
lines[i] = lines[i].replace('\n','')
#print lines
return lines

Also same search shows  Python-programming-exercises 100+ Python challenging programming exercises.  I  looked through codes and excercises and many are about loops and iterations, that is worthy to know and to look for reference. Important coding to become efficient and to integrate other libraries for files automation.

Solution not to loop infinitely with variables are below:

Python Sequence

https://www.python.org/dev/peps/pep-0212/#methods-for-sequence-objects

PEP 212 -- Loop Counter Iteration

Methods for sequence objects
This solution proposes the addition of indices, items and values methods to sequences, which enable looping over indices only, both indices and elements, and elements only respectively.

Loop counter iteration
The current idiom for looping over the indices makes use of the built-in range function:

for i in range(len(sequence)):
    # work with index i
Looping over both elements and indices can be achieved either by the old idiom or by using the new zip built-in function [2]:

for i in range(len(sequence)):
    e = sequence[i]
    # work with index i and element e
or:

for i, e in zip(range(len(sequence)), sequence):
   # work with index i and element e

This would immensely simplify the idioms for looping over indices and for looping over both elements and indices:

for i in sequence.indices():
    # work with index i

for i, e in sequence.items():
    # work with index i and element e
Additionally it would allow to do looping over the elements of sequences and dictionaries in a consistent way:

for e in sequence_or_dict.values():
    # do something with element e


No comments: