Python and JS
I decided it would help cement my understanding of both languages if I were to compare their syntax and functionality. While attempting to learn more about web development using HTML, CSS and JavaScript, I found a lot of similarities and also stumbled upon a few things that I didn’t understand while building Python projects. After doing some self-guided study about web development (CSS in this case), I’ve come to understand more about things like Bootstrap (in relation to its use with Python with projects powered by libraries like Flask or Django.)
The Basics:
Print and User Input
Python

JavaScript

Basic Loops and Conditionals
Python

JavaScript

Functions
Python

JavaScript

Specific Programming Language Features
String Manipulation
Python
word = “Hello World”
word = "Hello World" letter=word[0]
H
word = "Hello World" len(word)
11
word = "Hello World" >>> print word.count('l') # count how many times l is in the string 3 >>> print word.find("H") # find the word H in the string 0 >>> print word.index("World") # find the letters World in the string 6
s = "Count, the number of spaces" >>> print s.count(' ') 8
Slicing Use [ # : # ] to get set of letter
word = "Hello World" print word[0] #get one char of the word print word[0:1] #get one char of the word (same as above) print word[0:3] #get the first three char print word[:3] #get the first three char print word[-3:] #get the last three char print word[3:] #get all but the three first char print word[:-3] #get all but the three last character
word = "Hello World" word[start:end] # items start through end-1 word[start:] # items start through the rest of the list word[:end] # items from the beginning through end-1 word[:]
word = "Hello World" >>> word.split(' ') # Split on whitespace ['Hello', 'World']
word = "hello world" >>> word.startswith("H") True >>> word.endswith("d") True >>> word.endswith("w") False
print "."* 10 # prints ten dots >>> print "." * 10 ..........
word = "Hello World" >>> word.replace("Hello", "Goodbye") 'Goodbye World'
string = "Hello World" >>> print string.upper() HELLO WORLD >>> print string.lower() hello world >>> print string.title() Hello World >>> print string.capitalize() Hello world >>> print string.swapcase() hELLO wORLD
string = "Hello World" >>> print ' '.join(reversed(string)) d l r o W o l l e H
Python strings have the strip(), lstrip(), rstrip() methods for removing any character from both ends of a string. If the characters to be removed are not specified then white-space will be removed
word = "Hello World"
>>> print word.strip(' ') Hello World
strip() #removes from both ends lstrip() #removes leading characters (Left-strip) rstrip() #removes trailing characters (Right-strip) >>> word = " xyz " >>> print word xyz >>> print word.strip() xyz >>> print word.lstrip() xyz >>> print word.rstrip() xyz
"Hello " + "World" # = "Hello World" "Hello " + "World" + "!"# = "Hello World!"
>>> print ":".join(word) # #add a : between every char H:e:l:l:o: :W:o:r:l:d >>> print " ".join(word) # add a whitespace between every char H e l l o W o r l d
JavaScript
var word = “Hello World”;
var str = "HELLO WORLD";
var res = str.charAt(0);
H
var str = "Hello World!";
var n = str.length;
11
word = "Hello World" var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/g); var str = "Visit W3Schools!";
var n = str.search("W3Schools");
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
var str = “Hello world!”;
var res = str.slice(1, 5);
var str = “How are you doing today?”;
var res = str.split(” “);
var str = "Hello world, welcome to the universe.";
var n = str.startsWith("Hello");
var str = "Hello world, welcome to the universe.";
var n = str.endsWith("universe.");
var str = "Hello world!";
str.repeat(2);
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
var str = "Hello World!";
var res = str.toUpperCase();
var str = "Hello World!";
var res = str.toLowerCase();
function reverse(str){
let reversed = "";
for(let char of str){
reversed = char + reversed;
}
return reversed;
}
//Removes whitespace from both sides of string only
var str = ” Hello World! “;
alert(str.trim());
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
var elements = [‘Fire’, ‘Wind’, ‘Rain’];
console.log(elements.join());
// expected output: Fire,Wind,Rain
console.log(elements.join(”));
// expected output: FireWindRain
console.log(elements.join(‘-‘));
// expected output: Fire-Wind-Rain