DEV Community

Joshua Garvey
Joshua Garvey

Posted on

From JavaScript to Python - Day 1

I'm going to be converting my notes as I learn Python into blog posts by comparing it to a language I already know - JavaScript. I'm starting with freeCodeCamp’s Scientific Computing with Python course, diving straight into string manipulation and building a cipher.

Variables: No More let or const

In JavaScript, we explicitly declare variables using let, const, or (though not recommended) var:

// JavaScript Example
let number = 5;
const greeting = "Hello!";
Enter fullscreen mode Exit fullscreen mode

Python, on the other hand, skips the keyword entirely—just assign a value and you’re good to go:

# Python Example
number = 5
greeting = "Hello!"
Enter fullscreen mode Exit fullscreen mode

Strings: Mostly the Same

Strings in Python and JavaScript are similar in how they’re created:

// JavaScript Example
let str1 = "I'm a string in JavaScript";
let str2 = 'So am I';
Enter fullscreen mode Exit fullscreen mode
# Python Example
str1 = "I'm a string in python"
str2 = 'So am I'
Enter fullscreen mode Exit fullscreen mode

However, I did read that Python doesn't use template literals like in JavaScript but this isn't in the lesson so we will come back to it.

Printing: print() vs console.log()

Printing is straightforward in both languages:

// JavaScript Example
console.log("Hello, JavaScript world!");
Enter fullscreen mode Exit fullscreen mode
# Python Example
print("Hello, python world!")
Enter fullscreen mode Exit fullscreen mode

String Indexing: Familiar, But With a Bonus

String indexing is essentially the same as in JavaScript:

// JavaScript Example
let text = "Hello World";
console.log(text[8]); // "r"
Enter fullscreen mode Exit fullscreen mode
# Python Example
text = "Hello World"
print(text[8])  # "r"
Enter fullscreen mode Exit fullscreen mode

However, Python allows negative indices, which makes grabbing the last character much simpler:

# Python Example
print(text[-1])  # "d"
Enter fullscreen mode Exit fullscreen mode

In JavaScript, you’d have to do:

// JavaScript Example
console.log(text[text.length - 1]); // "d"
Enter fullscreen mode Exit fullscreen mode

This is a nice Python feature that makes things a little cleaner.

Finding String Length & Type

Python’s len() function is similar to JavaScript’s .length property:

// JavaScript Example
console.log("Hello".length); // 5
Enter fullscreen mode Exit fullscreen mode
# Python Example
print(len("Hello"))  # 5
Enter fullscreen mode Exit fullscreen mode

However, Python requires a function call (len()), whereas .length is a property in JavaScript.

Checking a variable’s type is also slightly different:

// JavaScript Example
console.log(typeof "Hello"); // "string"
Enter fullscreen mode Exit fullscreen mode
# Python Example
print(type("Hello"))  # <class 'str'>
Enter fullscreen mode Exit fullscreen mode

Methods vs Functions: The .find() Method

Like JavaScript, Python has built-in methods for string manipulation. I used .find() to locate characters in a string, which works similarly to JavaScript’s .indexOf():

// JavaScript Example
console.log("My brain hurts!".indexOf("r")); // 4
Enter fullscreen mode Exit fullscreen mode
# Python Example
sentence = "My brain hurts!"
print(sentence.find("r"))  # 4
Enter fullscreen mode Exit fullscreen mode

Both return the index of the first occurrence of a character.

Caesar Cipher & The Power of Python’s Simplicity

The first project was implementing a Caesar cipher, where each letter in a message is shifted by a fixed amount in the alphabet.

This involved:

  • Finding a letter’s position using .find()
  • Shifting the position by a set value
  • Grabbing the new letter using indexing

One key thing I learned was that Python’s string methods are case-sensitive. Running .find() on an uppercase letter failed since my alphabet string only contained lowercase letters. The fix? Using .lower() to convert the text before searching:

# Python Example
text = "Hello World"
alphabet = "abcdefghijklmnopqrstuvwxyz"
index = alphabet.find(text[0].lower())
print(index)  # 7
Enter fullscreen mode Exit fullscreen mode

This is where we paused - We will continue with this lesson on Day 2.

Top comments (0)